gspca.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  1. /*
  2. * Main USB camera driver
  3. *
  4. * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #define MODULE_NAME "gspca"
  21. #include <linux/init.h>
  22. #include <linux/version.h>
  23. #include <linux/fs.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/mm.h>
  28. #include <linux/string.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/io.h>
  31. #include <asm/page.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/jiffies.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include "gspca.h"
  36. /* global values */
  37. #define DEF_NURBS 2 /* default number of URBs */
  38. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  39. MODULE_DESCRIPTION("GSPCA USB Camera Driver");
  40. MODULE_LICENSE("GPL");
  41. #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 4, 0)
  42. static int video_nr = -1;
  43. #ifdef GSPCA_DEBUG
  44. int gspca_debug = D_ERR | D_PROBE;
  45. EXPORT_SYMBOL(gspca_debug);
  46. static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
  47. {
  48. if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
  49. PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
  50. txt,
  51. pixfmt & 0xff,
  52. (pixfmt >> 8) & 0xff,
  53. (pixfmt >> 16) & 0xff,
  54. pixfmt >> 24,
  55. w, h);
  56. } else {
  57. PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
  58. txt,
  59. pixfmt,
  60. w, h);
  61. }
  62. }
  63. #else
  64. #define PDEBUG_MODE(txt, pixfmt, w, h)
  65. #endif
  66. /* specific memory types - !! should different from V4L2_MEMORY_xxx */
  67. #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
  68. #define GSPCA_MEMORY_READ 7
  69. #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
  70. /*
  71. * VMA operations.
  72. */
  73. static void gspca_vm_open(struct vm_area_struct *vma)
  74. {
  75. struct gspca_frame *frame = vma->vm_private_data;
  76. frame->vma_use_count++;
  77. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
  78. }
  79. static void gspca_vm_close(struct vm_area_struct *vma)
  80. {
  81. struct gspca_frame *frame = vma->vm_private_data;
  82. if (--frame->vma_use_count <= 0)
  83. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  84. }
  85. static struct vm_operations_struct gspca_vm_ops = {
  86. .open = gspca_vm_open,
  87. .close = gspca_vm_close,
  88. };
  89. /* get the current input frame buffer */
  90. struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
  91. {
  92. struct gspca_frame *frame;
  93. int i;
  94. i = gspca_dev->fr_i;
  95. i = gspca_dev->fr_queue[i];
  96. frame = &gspca_dev->frame[i];
  97. if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
  98. != V4L2_BUF_FLAG_QUEUED)
  99. return NULL;
  100. return frame;
  101. }
  102. EXPORT_SYMBOL(gspca_get_i_frame);
  103. /*
  104. * fill a video frame from an URB and resubmit
  105. */
  106. static void fill_frame(struct gspca_dev *gspca_dev,
  107. struct urb *urb)
  108. {
  109. struct gspca_frame *frame;
  110. __u8 *data; /* address of data in the iso message */
  111. int i, len, st;
  112. cam_pkt_op pkt_scan;
  113. if (urb->status != 0) {
  114. #ifdef CONFIG_PM
  115. if (!gspca_dev->frozen)
  116. #endif
  117. PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
  118. return; /* disconnection ? */
  119. }
  120. pkt_scan = gspca_dev->sd_desc->pkt_scan;
  121. for (i = 0; i < urb->number_of_packets; i++) {
  122. /* check the availability of the frame buffer */
  123. frame = gspca_get_i_frame(gspca_dev);
  124. if (!frame) {
  125. gspca_dev->last_packet_type = DISCARD_PACKET;
  126. break;
  127. }
  128. /* check the packet status and length */
  129. len = urb->iso_frame_desc[i].actual_length;
  130. if (len == 0) {
  131. if (gspca_dev->empty_packet == 0)
  132. gspca_dev->empty_packet = 1;
  133. continue;
  134. }
  135. st = urb->iso_frame_desc[i].status;
  136. if (st) {
  137. PDEBUG(D_ERR,
  138. "ISOC data error: [%d] len=%d, status=%d",
  139. i, len, st);
  140. gspca_dev->last_packet_type = DISCARD_PACKET;
  141. continue;
  142. }
  143. /* let the packet be analyzed by the subdriver */
  144. PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
  145. i, urb->iso_frame_desc[i].offset, len);
  146. data = (__u8 *) urb->transfer_buffer
  147. + urb->iso_frame_desc[i].offset;
  148. pkt_scan(gspca_dev, frame, data, len);
  149. }
  150. /* resubmit the URB */
  151. st = usb_submit_urb(urb, GFP_ATOMIC);
  152. if (st < 0)
  153. PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
  154. }
  155. /*
  156. * ISOC message interrupt from the USB device
  157. *
  158. * Analyse each packet and call the subdriver for copy to the frame buffer.
  159. */
  160. static void isoc_irq(struct urb *urb
  161. )
  162. {
  163. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  164. PDEBUG(D_PACK, "isoc irq");
  165. if (!gspca_dev->streaming)
  166. return;
  167. fill_frame(gspca_dev, urb);
  168. }
  169. /*
  170. * bulk message interrupt from the USB device
  171. */
  172. static void bulk_irq(struct urb *urb
  173. )
  174. {
  175. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  176. struct gspca_frame *frame;
  177. int st;
  178. PDEBUG(D_PACK, "bulk irq");
  179. if (!gspca_dev->streaming)
  180. return;
  181. switch (urb->status) {
  182. case 0:
  183. break;
  184. case -ECONNRESET:
  185. urb->status = 0;
  186. break;
  187. default:
  188. #ifdef CONFIG_PM
  189. if (!gspca_dev->frozen)
  190. #endif
  191. PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
  192. return; /* disconnection ? */
  193. }
  194. /* check the availability of the frame buffer */
  195. frame = gspca_get_i_frame(gspca_dev);
  196. if (!frame) {
  197. gspca_dev->last_packet_type = DISCARD_PACKET;
  198. } else {
  199. PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
  200. gspca_dev->sd_desc->pkt_scan(gspca_dev,
  201. frame,
  202. urb->transfer_buffer,
  203. urb->actual_length);
  204. }
  205. /* resubmit the URB */
  206. if (gspca_dev->cam.bulk_nurbs != 0) {
  207. st = usb_submit_urb(urb, GFP_ATOMIC);
  208. if (st < 0)
  209. PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
  210. }
  211. }
  212. /*
  213. * add data to the current frame
  214. *
  215. * This function is called by the subdrivers at interrupt level.
  216. *
  217. * To build a frame, these ones must add
  218. * - one FIRST_PACKET
  219. * - 0 or many INTER_PACKETs
  220. * - one LAST_PACKET
  221. * DISCARD_PACKET invalidates the whole frame.
  222. * On LAST_PACKET, a new frame is returned.
  223. */
  224. struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
  225. enum gspca_packet_type packet_type,
  226. struct gspca_frame *frame,
  227. const __u8 *data,
  228. int len)
  229. {
  230. int i, j;
  231. PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
  232. /* when start of a new frame, if the current frame buffer
  233. * is not queued, discard the whole frame */
  234. if (packet_type == FIRST_PACKET) {
  235. if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
  236. != V4L2_BUF_FLAG_QUEUED) {
  237. gspca_dev->last_packet_type = DISCARD_PACKET;
  238. return frame;
  239. }
  240. frame->data_end = frame->data;
  241. jiffies_to_timeval(get_jiffies_64(),
  242. &frame->v4l2_buf.timestamp);
  243. frame->v4l2_buf.sequence = ++gspca_dev->sequence;
  244. } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
  245. if (packet_type == LAST_PACKET)
  246. gspca_dev->last_packet_type = packet_type;
  247. return frame;
  248. }
  249. /* append the packet to the frame buffer */
  250. if (len > 0) {
  251. if (frame->data_end - frame->data + len
  252. > frame->v4l2_buf.length) {
  253. PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
  254. frame->data_end - frame->data + len,
  255. frame->v4l2_buf.length);
  256. packet_type = DISCARD_PACKET;
  257. } else {
  258. memcpy(frame->data_end, data, len);
  259. frame->data_end += len;
  260. }
  261. }
  262. gspca_dev->last_packet_type = packet_type;
  263. /* if last packet, wake up the application and advance in the queue */
  264. if (packet_type == LAST_PACKET) {
  265. frame->v4l2_buf.bytesused = frame->data_end - frame->data;
  266. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  267. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
  268. wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
  269. i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
  270. gspca_dev->fr_i = i;
  271. PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
  272. frame->v4l2_buf.bytesused,
  273. gspca_dev->fr_q,
  274. i,
  275. gspca_dev->fr_o);
  276. j = gspca_dev->fr_queue[i];
  277. frame = &gspca_dev->frame[j];
  278. }
  279. return frame;
  280. }
  281. EXPORT_SYMBOL(gspca_frame_add);
  282. static int gspca_is_compressed(__u32 format)
  283. {
  284. switch (format) {
  285. case V4L2_PIX_FMT_MJPEG:
  286. case V4L2_PIX_FMT_JPEG:
  287. case V4L2_PIX_FMT_SPCA561:
  288. case V4L2_PIX_FMT_PAC207:
  289. return 1;
  290. }
  291. return 0;
  292. }
  293. static void *rvmalloc(unsigned long size)
  294. {
  295. void *mem;
  296. unsigned long adr;
  297. mem = vmalloc_32(size);
  298. if (mem != NULL) {
  299. adr = (unsigned long) mem;
  300. while ((long) size > 0) {
  301. SetPageReserved(vmalloc_to_page((void *) adr));
  302. adr += PAGE_SIZE;
  303. size -= PAGE_SIZE;
  304. }
  305. }
  306. return mem;
  307. }
  308. static void rvfree(void *mem, long size)
  309. {
  310. unsigned long adr;
  311. adr = (unsigned long) mem;
  312. while (size > 0) {
  313. ClearPageReserved(vmalloc_to_page((void *) adr));
  314. adr += PAGE_SIZE;
  315. size -= PAGE_SIZE;
  316. }
  317. vfree(mem);
  318. }
  319. static int frame_alloc(struct gspca_dev *gspca_dev,
  320. unsigned int count)
  321. {
  322. struct gspca_frame *frame;
  323. unsigned int frsz;
  324. int i;
  325. i = gspca_dev->curr_mode;
  326. frsz = gspca_dev->cam.cam_mode[i].sizeimage;
  327. PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
  328. frsz = PAGE_ALIGN(frsz);
  329. gspca_dev->frsz = frsz;
  330. if (count > GSPCA_MAX_FRAMES)
  331. count = GSPCA_MAX_FRAMES;
  332. gspca_dev->frbuf = rvmalloc(frsz * count);
  333. if (!gspca_dev->frbuf) {
  334. err("frame alloc failed");
  335. return -ENOMEM;
  336. }
  337. gspca_dev->nframes = count;
  338. for (i = 0; i < count; i++) {
  339. frame = &gspca_dev->frame[i];
  340. frame->v4l2_buf.index = i;
  341. frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  342. frame->v4l2_buf.flags = 0;
  343. frame->v4l2_buf.field = V4L2_FIELD_NONE;
  344. frame->v4l2_buf.length = frsz;
  345. frame->v4l2_buf.memory = gspca_dev->memory;
  346. frame->v4l2_buf.sequence = 0;
  347. frame->data = frame->data_end =
  348. gspca_dev->frbuf + i * frsz;
  349. frame->v4l2_buf.m.offset = i * frsz;
  350. }
  351. gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
  352. gspca_dev->last_packet_type = DISCARD_PACKET;
  353. gspca_dev->sequence = 0;
  354. return 0;
  355. }
  356. static void frame_free(struct gspca_dev *gspca_dev)
  357. {
  358. int i;
  359. PDEBUG(D_STREAM, "frame free");
  360. if (gspca_dev->frbuf != NULL) {
  361. rvfree(gspca_dev->frbuf,
  362. gspca_dev->nframes * gspca_dev->frsz);
  363. gspca_dev->frbuf = NULL;
  364. for (i = 0; i < gspca_dev->nframes; i++)
  365. gspca_dev->frame[i].data = NULL;
  366. }
  367. gspca_dev->nframes = 0;
  368. }
  369. static void destroy_urbs(struct gspca_dev *gspca_dev)
  370. {
  371. struct urb *urb;
  372. unsigned int i;
  373. PDEBUG(D_STREAM, "kill transfer");
  374. for (i = 0; i < MAX_NURBS; i++) {
  375. urb = gspca_dev->urb[i];
  376. if (urb == NULL)
  377. break;
  378. BUG_ON(!gspca_dev->dev);
  379. gspca_dev->urb[i] = NULL;
  380. if (!gspca_dev->present)
  381. usb_kill_urb(urb);
  382. if (urb->transfer_buffer != NULL)
  383. usb_buffer_free(gspca_dev->dev,
  384. urb->transfer_buffer_length,
  385. urb->transfer_buffer,
  386. urb->transfer_dma);
  387. usb_free_urb(urb);
  388. }
  389. }
  390. /*
  391. * look for an input transfer endpoint in an alternate setting
  392. */
  393. static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
  394. __u8 epaddr,
  395. __u8 xfer)
  396. {
  397. struct usb_host_endpoint *ep;
  398. int i, attr;
  399. epaddr |= USB_DIR_IN;
  400. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  401. ep = &alt->endpoint[i];
  402. if (ep->desc.bEndpointAddress == epaddr) {
  403. attr = ep->desc.bmAttributes
  404. & USB_ENDPOINT_XFERTYPE_MASK;
  405. if (attr == xfer)
  406. return ep;
  407. break;
  408. }
  409. }
  410. return NULL;
  411. }
  412. /*
  413. * look for an input (isoc or bulk) endpoint
  414. *
  415. * The endpoint is defined by the subdriver.
  416. * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
  417. * This routine may be called many times when the bandwidth is too small
  418. * (the bandwidth is checked on urb submit).
  419. */
  420. static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
  421. {
  422. struct usb_interface *intf;
  423. struct usb_host_endpoint *ep;
  424. int i, ret;
  425. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  426. ep = NULL;
  427. i = gspca_dev->alt; /* previous alt setting */
  428. /* try isoc */
  429. while (--i > 0) { /* alt 0 is unusable */
  430. ep = alt_xfer(&intf->altsetting[i],
  431. gspca_dev->cam.epaddr,
  432. USB_ENDPOINT_XFER_ISOC);
  433. if (ep)
  434. break;
  435. }
  436. /* if no isoc, try bulk */
  437. if (ep == NULL) {
  438. ep = alt_xfer(&intf->altsetting[0],
  439. gspca_dev->cam.epaddr,
  440. USB_ENDPOINT_XFER_BULK);
  441. if (ep == NULL) {
  442. err("no transfer endpoint found");
  443. return NULL;
  444. }
  445. }
  446. PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
  447. i, ep->desc.bEndpointAddress);
  448. if (i > 0) {
  449. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
  450. if (ret < 0) {
  451. err("set interface err %d", ret);
  452. return NULL;
  453. }
  454. }
  455. gspca_dev->alt = i; /* memorize the current alt setting */
  456. return ep;
  457. }
  458. /*
  459. * create the URBs for image transfer
  460. */
  461. static int create_urbs(struct gspca_dev *gspca_dev,
  462. struct usb_host_endpoint *ep)
  463. {
  464. struct urb *urb;
  465. int n, nurbs, i, psize, npkt, bsize;
  466. /* calculate the packet size and the number of packets */
  467. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  468. if (gspca_dev->alt != 0) { /* isoc */
  469. /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
  470. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  471. npkt = ISO_MAX_SIZE / psize;
  472. if (npkt > ISO_MAX_PKT)
  473. npkt = ISO_MAX_PKT;
  474. bsize = psize * npkt;
  475. PDEBUG(D_STREAM,
  476. "isoc %d pkts size %d = bsize:%d",
  477. npkt, psize, bsize);
  478. nurbs = DEF_NURBS;
  479. } else { /* bulk */
  480. npkt = 0;
  481. bsize = gspca_dev->cam.bulk_size;
  482. if (bsize == 0)
  483. bsize = psize;
  484. PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
  485. if (gspca_dev->cam.bulk_nurbs != 0)
  486. nurbs = gspca_dev->cam.bulk_nurbs;
  487. else
  488. nurbs = 1;
  489. }
  490. gspca_dev->nurbs = nurbs;
  491. for (n = 0; n < nurbs; n++) {
  492. urb = usb_alloc_urb(npkt, GFP_KERNEL);
  493. if (!urb) {
  494. err("usb_alloc_urb failed");
  495. destroy_urbs(gspca_dev);
  496. return -ENOMEM;
  497. }
  498. urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
  499. bsize,
  500. GFP_KERNEL,
  501. &urb->transfer_dma);
  502. if (urb->transfer_buffer == NULL) {
  503. usb_free_urb(urb);
  504. err("usb_buffer_urb failed");
  505. destroy_urbs(gspca_dev);
  506. return -ENOMEM;
  507. }
  508. gspca_dev->urb[n] = urb;
  509. urb->dev = gspca_dev->dev;
  510. urb->context = gspca_dev;
  511. urb->transfer_buffer_length = bsize;
  512. if (npkt != 0) { /* ISOC */
  513. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  514. ep->desc.bEndpointAddress);
  515. urb->transfer_flags = URB_ISO_ASAP
  516. | URB_NO_TRANSFER_DMA_MAP;
  517. urb->interval = ep->desc.bInterval;
  518. urb->complete = isoc_irq;
  519. urb->number_of_packets = npkt;
  520. for (i = 0; i < npkt; i++) {
  521. urb->iso_frame_desc[i].length = psize;
  522. urb->iso_frame_desc[i].offset = psize * i;
  523. }
  524. } else { /* bulk */
  525. urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
  526. ep->desc.bEndpointAddress),
  527. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  528. urb->complete = bulk_irq;
  529. }
  530. }
  531. return 0;
  532. }
  533. /*
  534. * start the USB transfer
  535. */
  536. static int gspca_init_transfer(struct gspca_dev *gspca_dev)
  537. {
  538. struct usb_host_endpoint *ep;
  539. int n, ret;
  540. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  541. return -ERESTARTSYS;
  542. /* set the higher alternate setting and
  543. * loop until urb submit succeeds */
  544. gspca_dev->alt = gspca_dev->nbalt;
  545. for (;;) {
  546. PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
  547. ep = get_ep(gspca_dev);
  548. if (ep == NULL) {
  549. ret = -EIO;
  550. goto out;
  551. }
  552. ret = create_urbs(gspca_dev, ep);
  553. if (ret < 0)
  554. goto out;
  555. /* clear the bulk endpoint */
  556. if (gspca_dev->alt == 0) /* if bulk transfer */
  557. usb_clear_halt(gspca_dev->dev,
  558. usb_rcvintpipe(gspca_dev->dev,
  559. gspca_dev->cam.epaddr));
  560. /* start the cam */
  561. ret = gspca_dev->sd_desc->start(gspca_dev);
  562. if (ret < 0) {
  563. destroy_urbs(gspca_dev);
  564. goto out;
  565. }
  566. gspca_dev->streaming = 1;
  567. /* some bulk transfers are started by the subdriver */
  568. if (gspca_dev->alt == 0 && gspca_dev->cam.bulk_nurbs == 0)
  569. break;
  570. /* submit the URBs */
  571. for (n = 0; n < gspca_dev->nurbs; n++) {
  572. ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
  573. if (ret < 0) {
  574. PDEBUG(D_ERR|D_STREAM,
  575. "usb_submit_urb [%d] err %d", n, ret);
  576. gspca_dev->streaming = 0;
  577. destroy_urbs(gspca_dev);
  578. if (ret == -ENOSPC) {
  579. msleep(20); /* wait for kill
  580. * complete */
  581. break; /* try the previous alt */
  582. }
  583. goto out;
  584. }
  585. }
  586. if (ret >= 0)
  587. break;
  588. }
  589. out:
  590. mutex_unlock(&gspca_dev->usb_lock);
  591. return ret;
  592. }
  593. static int gspca_set_alt0(struct gspca_dev *gspca_dev)
  594. {
  595. int ret;
  596. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
  597. if (ret < 0)
  598. PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
  599. return ret;
  600. }
  601. /* Note: both the queue and the usb locks should be held when calling this */
  602. static void gspca_stream_off(struct gspca_dev *gspca_dev)
  603. {
  604. gspca_dev->streaming = 0;
  605. if (gspca_dev->present
  606. && gspca_dev->sd_desc->stopN)
  607. gspca_dev->sd_desc->stopN(gspca_dev);
  608. destroy_urbs(gspca_dev);
  609. gspca_set_alt0(gspca_dev);
  610. if (gspca_dev->sd_desc->stop0)
  611. gspca_dev->sd_desc->stop0(gspca_dev);
  612. PDEBUG(D_STREAM, "stream off OK");
  613. }
  614. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  615. {
  616. int i;
  617. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  618. gspca_dev->curr_mode = i;
  619. gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
  620. gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
  621. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
  622. }
  623. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  624. int width, int height)
  625. {
  626. int i;
  627. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  628. if (width >= gspca_dev->cam.cam_mode[i].width
  629. && height >= gspca_dev->cam.cam_mode[i].height)
  630. break;
  631. }
  632. return i;
  633. }
  634. /*
  635. * search a mode with the right pixel format
  636. */
  637. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  638. int mode,
  639. int pixfmt)
  640. {
  641. int modeU, modeD;
  642. modeU = modeD = mode;
  643. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  644. if (--modeD >= 0) {
  645. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  646. == pixfmt)
  647. return modeD;
  648. }
  649. if (++modeU < gspca_dev->cam.nmodes) {
  650. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  651. == pixfmt)
  652. return modeU;
  653. }
  654. }
  655. return -EINVAL;
  656. }
  657. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  658. struct v4l2_fmtdesc *fmtdesc)
  659. {
  660. struct gspca_dev *gspca_dev = priv;
  661. int i, j, index;
  662. __u32 fmt_tb[8];
  663. /* give an index to each format */
  664. index = 0;
  665. j = 0;
  666. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  667. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  668. j = 0;
  669. for (;;) {
  670. if (fmt_tb[j] == fmt_tb[index])
  671. break;
  672. j++;
  673. }
  674. if (j == index) {
  675. if (fmtdesc->index == index)
  676. break; /* new format */
  677. index++;
  678. if (index >= ARRAY_SIZE(fmt_tb))
  679. return -EINVAL;
  680. }
  681. }
  682. if (i < 0)
  683. return -EINVAL; /* no more format */
  684. fmtdesc->pixelformat = fmt_tb[index];
  685. if (gspca_is_compressed(fmt_tb[index]))
  686. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  687. fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  688. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  689. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  690. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  691. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  692. fmtdesc->description[4] = '\0';
  693. return 0;
  694. }
  695. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  696. struct v4l2_format *fmt)
  697. {
  698. struct gspca_dev *gspca_dev = priv;
  699. int mode;
  700. mode = gspca_dev->curr_mode;
  701. memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
  702. sizeof fmt->fmt.pix);
  703. return 0;
  704. }
  705. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  706. struct v4l2_format *fmt)
  707. {
  708. int w, h, mode, mode2;
  709. w = fmt->fmt.pix.width;
  710. h = fmt->fmt.pix.height;
  711. #ifdef GSPCA_DEBUG
  712. if (gspca_debug & D_CONF)
  713. PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
  714. #endif
  715. /* search the closest mode for width and height */
  716. mode = wxh_to_mode(gspca_dev, w, h);
  717. /* OK if right palette */
  718. if (gspca_dev->cam.cam_mode[mode].pixelformat
  719. != fmt->fmt.pix.pixelformat) {
  720. /* else, search the closest mode with the same pixel format */
  721. mode2 = gspca_get_mode(gspca_dev, mode,
  722. fmt->fmt.pix.pixelformat);
  723. if (mode2 >= 0)
  724. mode = mode2;
  725. /* else
  726. ; * no chance, return this mode */
  727. }
  728. memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
  729. sizeof fmt->fmt.pix);
  730. return mode; /* used when s_fmt */
  731. }
  732. static int vidioc_try_fmt_vid_cap(struct file *file,
  733. void *priv,
  734. struct v4l2_format *fmt)
  735. {
  736. struct gspca_dev *gspca_dev = priv;
  737. int ret;
  738. ret = try_fmt_vid_cap(gspca_dev, fmt);
  739. if (ret < 0)
  740. return ret;
  741. return 0;
  742. }
  743. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  744. struct v4l2_format *fmt)
  745. {
  746. struct gspca_dev *gspca_dev = priv;
  747. int ret;
  748. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  749. return -ERESTARTSYS;
  750. ret = try_fmt_vid_cap(gspca_dev, fmt);
  751. if (ret < 0)
  752. goto out;
  753. if (gspca_dev->nframes != 0
  754. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  755. ret = -EINVAL;
  756. goto out;
  757. }
  758. if (ret == gspca_dev->curr_mode) {
  759. ret = 0;
  760. goto out; /* same mode */
  761. }
  762. if (gspca_dev->streaming) {
  763. ret = -EBUSY;
  764. goto out;
  765. }
  766. gspca_dev->width = fmt->fmt.pix.width;
  767. gspca_dev->height = fmt->fmt.pix.height;
  768. gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
  769. gspca_dev->curr_mode = ret;
  770. ret = 0;
  771. out:
  772. mutex_unlock(&gspca_dev->queue_lock);
  773. return ret;
  774. }
  775. static void gspca_release(struct video_device *vfd)
  776. {
  777. struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
  778. PDEBUG(D_STREAM, "device released");
  779. kfree(gspca_dev->usb_buf);
  780. kfree(gspca_dev);
  781. }
  782. static int dev_open(struct file *file)
  783. {
  784. struct gspca_dev *gspca_dev;
  785. int ret;
  786. PDEBUG(D_STREAM, "%s open", current->comm);
  787. gspca_dev = (struct gspca_dev *) video_devdata(file);
  788. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  789. return -ERESTARTSYS;
  790. if (!gspca_dev->present) {
  791. ret = -ENODEV;
  792. goto out;
  793. }
  794. if (gspca_dev->users > 4) { /* (arbitrary value) */
  795. ret = -EBUSY;
  796. goto out;
  797. }
  798. /* protect the subdriver against rmmod */
  799. if (!try_module_get(gspca_dev->module)) {
  800. ret = -ENODEV;
  801. goto out;
  802. }
  803. gspca_dev->users++;
  804. file->private_data = gspca_dev;
  805. #ifdef GSPCA_DEBUG
  806. /* activate the v4l2 debug */
  807. if (gspca_debug & D_V4L2)
  808. gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
  809. | V4L2_DEBUG_IOCTL_ARG;
  810. else
  811. gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
  812. | V4L2_DEBUG_IOCTL_ARG);
  813. #endif
  814. ret = 0;
  815. out:
  816. mutex_unlock(&gspca_dev->queue_lock);
  817. if (ret != 0)
  818. PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
  819. else
  820. PDEBUG(D_STREAM, "open done");
  821. return ret;
  822. }
  823. static int dev_close(struct file *file)
  824. {
  825. struct gspca_dev *gspca_dev = file->private_data;
  826. PDEBUG(D_STREAM, "%s close", current->comm);
  827. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  828. return -ERESTARTSYS;
  829. gspca_dev->users--;
  830. /* if the file did the capture, free the streaming resources */
  831. if (gspca_dev->capt_file == file) {
  832. if (gspca_dev->streaming) {
  833. mutex_lock(&gspca_dev->usb_lock);
  834. gspca_stream_off(gspca_dev);
  835. mutex_unlock(&gspca_dev->usb_lock);
  836. }
  837. frame_free(gspca_dev);
  838. gspca_dev->capt_file = NULL;
  839. gspca_dev->memory = GSPCA_MEMORY_NO;
  840. }
  841. file->private_data = NULL;
  842. module_put(gspca_dev->module);
  843. mutex_unlock(&gspca_dev->queue_lock);
  844. PDEBUG(D_STREAM, "close done");
  845. return 0;
  846. }
  847. static int vidioc_querycap(struct file *file, void *priv,
  848. struct v4l2_capability *cap)
  849. {
  850. struct gspca_dev *gspca_dev = priv;
  851. memset(cap, 0, sizeof *cap);
  852. strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
  853. if (gspca_dev->dev->product != NULL) {
  854. strncpy(cap->card, gspca_dev->dev->product,
  855. sizeof cap->card);
  856. } else {
  857. snprintf(cap->card, sizeof cap->card,
  858. "USB Camera (%04x:%04x)",
  859. le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
  860. le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
  861. }
  862. strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
  863. sizeof cap->bus_info);
  864. cap->version = DRIVER_VERSION_NUMBER;
  865. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  866. | V4L2_CAP_STREAMING
  867. | V4L2_CAP_READWRITE;
  868. return 0;
  869. }
  870. static int vidioc_queryctrl(struct file *file, void *priv,
  871. struct v4l2_queryctrl *q_ctrl)
  872. {
  873. struct gspca_dev *gspca_dev = priv;
  874. int i, ix;
  875. u32 id;
  876. ix = -1;
  877. id = q_ctrl->id;
  878. if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  879. id &= V4L2_CTRL_ID_MASK;
  880. id++;
  881. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  882. if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
  883. continue;
  884. if (ix < 0) {
  885. ix = i;
  886. continue;
  887. }
  888. if (gspca_dev->sd_desc->ctrls[i].qctrl.id
  889. > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
  890. continue;
  891. ix = i;
  892. }
  893. }
  894. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  895. if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
  896. ix = i;
  897. break;
  898. }
  899. }
  900. if (ix < 0)
  901. return -EINVAL;
  902. memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
  903. sizeof *q_ctrl);
  904. if (gspca_dev->ctrl_dis & (1 << ix))
  905. q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  906. return 0;
  907. }
  908. static int vidioc_s_ctrl(struct file *file, void *priv,
  909. struct v4l2_control *ctrl)
  910. {
  911. struct gspca_dev *gspca_dev = priv;
  912. const struct ctrl *ctrls;
  913. int i, ret;
  914. for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
  915. i < gspca_dev->sd_desc->nctrls;
  916. i++, ctrls++) {
  917. if (ctrl->id != ctrls->qctrl.id)
  918. continue;
  919. if (gspca_dev->ctrl_dis & (1 << i))
  920. return -EINVAL;
  921. if (ctrl->value < ctrls->qctrl.minimum
  922. || ctrl->value > ctrls->qctrl.maximum)
  923. return -ERANGE;
  924. PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
  925. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  926. return -ERESTARTSYS;
  927. ret = ctrls->set(gspca_dev, ctrl->value);
  928. mutex_unlock(&gspca_dev->usb_lock);
  929. return ret;
  930. }
  931. return -EINVAL;
  932. }
  933. static int vidioc_g_ctrl(struct file *file, void *priv,
  934. struct v4l2_control *ctrl)
  935. {
  936. struct gspca_dev *gspca_dev = priv;
  937. const struct ctrl *ctrls;
  938. int i, ret;
  939. for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
  940. i < gspca_dev->sd_desc->nctrls;
  941. i++, ctrls++) {
  942. if (ctrl->id != ctrls->qctrl.id)
  943. continue;
  944. if (gspca_dev->ctrl_dis & (1 << i))
  945. return -EINVAL;
  946. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  947. return -ERESTARTSYS;
  948. ret = ctrls->get(gspca_dev, &ctrl->value);
  949. mutex_unlock(&gspca_dev->usb_lock);
  950. return ret;
  951. }
  952. return -EINVAL;
  953. }
  954. /*fixme: have an audio flag in gspca_dev?*/
  955. static int vidioc_s_audio(struct file *file, void *priv,
  956. struct v4l2_audio *audio)
  957. {
  958. if (audio->index != 0)
  959. return -EINVAL;
  960. return 0;
  961. }
  962. static int vidioc_g_audio(struct file *file, void *priv,
  963. struct v4l2_audio *audio)
  964. {
  965. memset(audio, 0, sizeof *audio);
  966. strcpy(audio->name, "Microphone");
  967. return 0;
  968. }
  969. static int vidioc_enumaudio(struct file *file, void *priv,
  970. struct v4l2_audio *audio)
  971. {
  972. if (audio->index != 0)
  973. return -EINVAL;
  974. strcpy(audio->name, "Microphone");
  975. audio->capability = 0;
  976. audio->mode = 0;
  977. return 0;
  978. }
  979. static int vidioc_querymenu(struct file *file, void *priv,
  980. struct v4l2_querymenu *qmenu)
  981. {
  982. struct gspca_dev *gspca_dev = priv;
  983. if (!gspca_dev->sd_desc->querymenu)
  984. return -EINVAL;
  985. return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
  986. }
  987. static int vidioc_enum_input(struct file *file, void *priv,
  988. struct v4l2_input *input)
  989. {
  990. struct gspca_dev *gspca_dev = priv;
  991. if (input->index != 0)
  992. return -EINVAL;
  993. memset(input, 0, sizeof *input);
  994. input->type = V4L2_INPUT_TYPE_CAMERA;
  995. strncpy(input->name, gspca_dev->sd_desc->name,
  996. sizeof input->name);
  997. return 0;
  998. }
  999. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1000. {
  1001. *i = 0;
  1002. return 0;
  1003. }
  1004. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1005. {
  1006. if (i > 0)
  1007. return -EINVAL;
  1008. return (0);
  1009. }
  1010. static int vidioc_reqbufs(struct file *file, void *priv,
  1011. struct v4l2_requestbuffers *rb)
  1012. {
  1013. struct gspca_dev *gspca_dev = priv;
  1014. int i, ret = 0;
  1015. switch (rb->memory) {
  1016. case GSPCA_MEMORY_READ: /* (internal call) */
  1017. case V4L2_MEMORY_MMAP:
  1018. case V4L2_MEMORY_USERPTR:
  1019. break;
  1020. default:
  1021. return -EINVAL;
  1022. }
  1023. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1024. return -ERESTARTSYS;
  1025. if (gspca_dev->memory != GSPCA_MEMORY_NO
  1026. && gspca_dev->memory != rb->memory) {
  1027. ret = -EBUSY;
  1028. goto out;
  1029. }
  1030. /* only one file may do the capture */
  1031. if (gspca_dev->capt_file != NULL
  1032. && gspca_dev->capt_file != file) {
  1033. ret = -EBUSY;
  1034. goto out;
  1035. }
  1036. /* if allocated, the buffers must not be mapped */
  1037. for (i = 0; i < gspca_dev->nframes; i++) {
  1038. if (gspca_dev->frame[i].vma_use_count) {
  1039. ret = -EBUSY;
  1040. goto out;
  1041. }
  1042. }
  1043. /* stop streaming */
  1044. if (gspca_dev->streaming) {
  1045. mutex_lock(&gspca_dev->usb_lock);
  1046. gspca_stream_off(gspca_dev);
  1047. mutex_unlock(&gspca_dev->usb_lock);
  1048. }
  1049. /* free the previous allocated buffers, if any */
  1050. if (gspca_dev->nframes != 0) {
  1051. frame_free(gspca_dev);
  1052. gspca_dev->capt_file = NULL;
  1053. }
  1054. if (rb->count == 0) /* unrequest */
  1055. goto out;
  1056. gspca_dev->memory = rb->memory;
  1057. ret = frame_alloc(gspca_dev, rb->count);
  1058. if (ret == 0) {
  1059. rb->count = gspca_dev->nframes;
  1060. gspca_dev->capt_file = file;
  1061. }
  1062. out:
  1063. mutex_unlock(&gspca_dev->queue_lock);
  1064. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  1065. return ret;
  1066. }
  1067. static int vidioc_querybuf(struct file *file, void *priv,
  1068. struct v4l2_buffer *v4l2_buf)
  1069. {
  1070. struct gspca_dev *gspca_dev = priv;
  1071. struct gspca_frame *frame;
  1072. if (v4l2_buf->index < 0
  1073. || v4l2_buf->index >= gspca_dev->nframes)
  1074. return -EINVAL;
  1075. frame = &gspca_dev->frame[v4l2_buf->index];
  1076. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1077. return 0;
  1078. }
  1079. static int vidioc_streamon(struct file *file, void *priv,
  1080. enum v4l2_buf_type buf_type)
  1081. {
  1082. struct gspca_dev *gspca_dev = priv;
  1083. int ret;
  1084. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1085. return -EINVAL;
  1086. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1087. return -ERESTARTSYS;
  1088. if (!gspca_dev->present) {
  1089. ret = -ENODEV;
  1090. goto out;
  1091. }
  1092. if (gspca_dev->nframes == 0
  1093. || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
  1094. ret = -EINVAL;
  1095. goto out;
  1096. }
  1097. if (!gspca_dev->streaming) {
  1098. ret = gspca_init_transfer(gspca_dev);
  1099. if (ret < 0)
  1100. goto out;
  1101. }
  1102. #ifdef GSPCA_DEBUG
  1103. if (gspca_debug & D_STREAM) {
  1104. PDEBUG_MODE("stream on OK",
  1105. gspca_dev->pixfmt,
  1106. gspca_dev->width,
  1107. gspca_dev->height);
  1108. }
  1109. #endif
  1110. ret = 0;
  1111. out:
  1112. mutex_unlock(&gspca_dev->queue_lock);
  1113. return ret;
  1114. }
  1115. static int vidioc_streamoff(struct file *file, void *priv,
  1116. enum v4l2_buf_type buf_type)
  1117. {
  1118. struct gspca_dev *gspca_dev = priv;
  1119. int i, ret;
  1120. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1121. return -EINVAL;
  1122. if (!gspca_dev->streaming)
  1123. return 0;
  1124. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1125. return -ERESTARTSYS;
  1126. /* stop streaming */
  1127. if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
  1128. ret = -ERESTARTSYS;
  1129. goto out;
  1130. }
  1131. gspca_stream_off(gspca_dev);
  1132. mutex_unlock(&gspca_dev->usb_lock);
  1133. /* empty the application queues */
  1134. for (i = 0; i < gspca_dev->nframes; i++)
  1135. gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
  1136. gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
  1137. gspca_dev->last_packet_type = DISCARD_PACKET;
  1138. gspca_dev->sequence = 0;
  1139. ret = 0;
  1140. out:
  1141. mutex_unlock(&gspca_dev->queue_lock);
  1142. return ret;
  1143. }
  1144. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1145. struct v4l2_jpegcompression *jpegcomp)
  1146. {
  1147. struct gspca_dev *gspca_dev = priv;
  1148. int ret;
  1149. if (!gspca_dev->sd_desc->get_jcomp)
  1150. return -EINVAL;
  1151. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1152. return -ERESTARTSYS;
  1153. ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1154. mutex_unlock(&gspca_dev->usb_lock);
  1155. return ret;
  1156. }
  1157. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1158. struct v4l2_jpegcompression *jpegcomp)
  1159. {
  1160. struct gspca_dev *gspca_dev = priv;
  1161. int ret;
  1162. if (!gspca_dev->sd_desc->set_jcomp)
  1163. return -EINVAL;
  1164. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1165. return -ERESTARTSYS;
  1166. ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1167. mutex_unlock(&gspca_dev->usb_lock);
  1168. return ret;
  1169. }
  1170. static int vidioc_g_parm(struct file *filp, void *priv,
  1171. struct v4l2_streamparm *parm)
  1172. {
  1173. struct gspca_dev *gspca_dev = priv;
  1174. memset(parm, 0, sizeof *parm);
  1175. parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1176. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1177. if (gspca_dev->sd_desc->get_streamparm) {
  1178. int ret;
  1179. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1180. return -ERESTARTSYS;
  1181. ret = gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
  1182. mutex_unlock(&gspca_dev->usb_lock);
  1183. return ret;
  1184. }
  1185. return 0;
  1186. }
  1187. static int vidioc_s_parm(struct file *filp, void *priv,
  1188. struct v4l2_streamparm *parm)
  1189. {
  1190. struct gspca_dev *gspca_dev = priv;
  1191. int n;
  1192. n = parm->parm.capture.readbuffers;
  1193. if (n == 0 || n > GSPCA_MAX_FRAMES)
  1194. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1195. else
  1196. gspca_dev->nbufread = n;
  1197. if (gspca_dev->sd_desc->set_streamparm) {
  1198. int ret;
  1199. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1200. return -ERESTARTSYS;
  1201. ret = gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
  1202. mutex_unlock(&gspca_dev->usb_lock);
  1203. return ret;
  1204. }
  1205. return 0;
  1206. }
  1207. static int vidioc_s_std(struct file *filp, void *priv,
  1208. v4l2_std_id *parm)
  1209. {
  1210. return 0;
  1211. }
  1212. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1213. static int vidiocgmbuf(struct file *file, void *priv,
  1214. struct video_mbuf *mbuf)
  1215. {
  1216. struct gspca_dev *gspca_dev = file->private_data;
  1217. int i;
  1218. PDEBUG(D_STREAM, "cgmbuf");
  1219. if (gspca_dev->nframes == 0) {
  1220. int ret;
  1221. {
  1222. struct v4l2_format fmt;
  1223. memset(&fmt, 0, sizeof fmt);
  1224. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1225. i = gspca_dev->cam.nmodes - 1; /* highest mode */
  1226. fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
  1227. fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
  1228. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
  1229. ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
  1230. if (ret != 0)
  1231. return ret;
  1232. }
  1233. {
  1234. struct v4l2_requestbuffers rb;
  1235. memset(&rb, 0, sizeof rb);
  1236. rb.count = 4;
  1237. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1238. rb.memory = V4L2_MEMORY_MMAP;
  1239. ret = vidioc_reqbufs(file, priv, &rb);
  1240. if (ret != 0)
  1241. return ret;
  1242. }
  1243. }
  1244. mbuf->frames = gspca_dev->nframes;
  1245. mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
  1246. for (i = 0; i < mbuf->frames; i++)
  1247. mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
  1248. return 0;
  1249. }
  1250. #endif
  1251. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1252. {
  1253. struct gspca_dev *gspca_dev = file->private_data;
  1254. struct gspca_frame *frame;
  1255. struct page *page;
  1256. unsigned long addr, start, size;
  1257. int i, ret;
  1258. start = vma->vm_start;
  1259. size = vma->vm_end - vma->vm_start;
  1260. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1261. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1262. return -ERESTARTSYS;
  1263. if (!gspca_dev->present) {
  1264. ret = -ENODEV;
  1265. goto out;
  1266. }
  1267. if (gspca_dev->capt_file != file) {
  1268. ret = -EINVAL;
  1269. goto out;
  1270. }
  1271. frame = NULL;
  1272. for (i = 0; i < gspca_dev->nframes; ++i) {
  1273. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1274. PDEBUG(D_STREAM, "mmap bad memory type");
  1275. break;
  1276. }
  1277. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1278. == vma->vm_pgoff) {
  1279. frame = &gspca_dev->frame[i];
  1280. break;
  1281. }
  1282. }
  1283. if (frame == NULL) {
  1284. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1285. ret = -EINVAL;
  1286. goto out;
  1287. }
  1288. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1289. /* v4l1 maps all the buffers */
  1290. if (i != 0
  1291. || size != frame->v4l2_buf.length * gspca_dev->nframes)
  1292. #endif
  1293. if (size != frame->v4l2_buf.length) {
  1294. PDEBUG(D_STREAM, "mmap bad size");
  1295. ret = -EINVAL;
  1296. goto out;
  1297. }
  1298. /*
  1299. * - VM_IO marks the area as being a mmaped region for I/O to a
  1300. * device. It also prevents the region from being core dumped.
  1301. */
  1302. vma->vm_flags |= VM_IO;
  1303. addr = (unsigned long) frame->data;
  1304. while (size > 0) {
  1305. page = vmalloc_to_page((void *) addr);
  1306. ret = vm_insert_page(vma, start, page);
  1307. if (ret < 0)
  1308. goto out;
  1309. start += PAGE_SIZE;
  1310. addr += PAGE_SIZE;
  1311. size -= PAGE_SIZE;
  1312. }
  1313. vma->vm_ops = &gspca_vm_ops;
  1314. vma->vm_private_data = frame;
  1315. gspca_vm_open(vma);
  1316. ret = 0;
  1317. out:
  1318. mutex_unlock(&gspca_dev->queue_lock);
  1319. return ret;
  1320. }
  1321. /*
  1322. * wait for a video frame
  1323. *
  1324. * If a frame is ready, its index is returned.
  1325. */
  1326. static int frame_wait(struct gspca_dev *gspca_dev,
  1327. int nonblock_ing)
  1328. {
  1329. struct gspca_frame *frame;
  1330. int i, j, ret;
  1331. /* check if a frame is ready */
  1332. i = gspca_dev->fr_o;
  1333. j = gspca_dev->fr_queue[i];
  1334. frame = &gspca_dev->frame[j];
  1335. if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
  1336. if (nonblock_ing)
  1337. return -EAGAIN;
  1338. /* wait till a frame is ready */
  1339. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1340. (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
  1341. !gspca_dev->streaming || !gspca_dev->present,
  1342. msecs_to_jiffies(3000));
  1343. if (ret < 0)
  1344. return ret;
  1345. if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
  1346. return -EIO;
  1347. }
  1348. gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
  1349. PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
  1350. gspca_dev->fr_q,
  1351. gspca_dev->fr_i,
  1352. gspca_dev->fr_o);
  1353. if (gspca_dev->sd_desc->dq_callback) {
  1354. mutex_lock(&gspca_dev->usb_lock);
  1355. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1356. mutex_unlock(&gspca_dev->usb_lock);
  1357. }
  1358. return j;
  1359. }
  1360. /*
  1361. * dequeue a video buffer
  1362. *
  1363. * If nonblock_ing is false, block until a buffer is available.
  1364. */
  1365. static int vidioc_dqbuf(struct file *file, void *priv,
  1366. struct v4l2_buffer *v4l2_buf)
  1367. {
  1368. struct gspca_dev *gspca_dev = priv;
  1369. struct gspca_frame *frame;
  1370. int i, ret;
  1371. PDEBUG(D_FRAM, "dqbuf");
  1372. if (v4l2_buf->memory != gspca_dev->memory)
  1373. return -EINVAL;
  1374. /* if not streaming, be sure the application will not loop forever */
  1375. if (!(file->f_flags & O_NONBLOCK)
  1376. && !gspca_dev->streaming && gspca_dev->users == 1)
  1377. return -EINVAL;
  1378. /* only the capturing file may dequeue */
  1379. if (gspca_dev->capt_file != file)
  1380. return -EINVAL;
  1381. /* only one dequeue / read at a time */
  1382. if (mutex_lock_interruptible(&gspca_dev->read_lock))
  1383. return -ERESTARTSYS;
  1384. ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
  1385. if (ret < 0)
  1386. goto out;
  1387. i = ret; /* frame index */
  1388. frame = &gspca_dev->frame[i];
  1389. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1390. if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
  1391. frame->data,
  1392. frame->v4l2_buf.bytesused)) {
  1393. PDEBUG(D_ERR|D_STREAM,
  1394. "dqbuf cp to user failed");
  1395. ret = -EFAULT;
  1396. goto out;
  1397. }
  1398. }
  1399. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1400. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1401. PDEBUG(D_FRAM, "dqbuf %d", i);
  1402. ret = 0;
  1403. out:
  1404. mutex_unlock(&gspca_dev->read_lock);
  1405. return ret;
  1406. }
  1407. /*
  1408. * queue a video buffer
  1409. *
  1410. * Attempting to queue a buffer that has already been
  1411. * queued will return -EINVAL.
  1412. */
  1413. static int vidioc_qbuf(struct file *file, void *priv,
  1414. struct v4l2_buffer *v4l2_buf)
  1415. {
  1416. struct gspca_dev *gspca_dev = priv;
  1417. struct gspca_frame *frame;
  1418. int i, index, ret;
  1419. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1420. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1421. return -ERESTARTSYS;
  1422. index = v4l2_buf->index;
  1423. if ((unsigned) index >= gspca_dev->nframes) {
  1424. PDEBUG(D_FRAM,
  1425. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1426. ret = -EINVAL;
  1427. goto out;
  1428. }
  1429. if (v4l2_buf->memory != gspca_dev->memory) {
  1430. PDEBUG(D_FRAM, "qbuf bad memory type");
  1431. ret = -EINVAL;
  1432. goto out;
  1433. }
  1434. frame = &gspca_dev->frame[index];
  1435. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1436. PDEBUG(D_FRAM, "qbuf bad state");
  1437. ret = -EINVAL;
  1438. goto out;
  1439. }
  1440. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1441. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1442. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1443. frame->v4l2_buf.length = v4l2_buf->length;
  1444. }
  1445. /* put the buffer in the 'queued' queue */
  1446. i = gspca_dev->fr_q;
  1447. gspca_dev->fr_queue[i] = index;
  1448. gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
  1449. PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
  1450. gspca_dev->fr_q,
  1451. gspca_dev->fr_i,
  1452. gspca_dev->fr_o);
  1453. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1454. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1455. ret = 0;
  1456. out:
  1457. mutex_unlock(&gspca_dev->queue_lock);
  1458. return ret;
  1459. }
  1460. /*
  1461. * allocate the resources for read()
  1462. */
  1463. static int read_alloc(struct gspca_dev *gspca_dev,
  1464. struct file *file)
  1465. {
  1466. struct v4l2_buffer v4l2_buf;
  1467. int i, ret;
  1468. PDEBUG(D_STREAM, "read alloc");
  1469. if (gspca_dev->nframes == 0) {
  1470. struct v4l2_requestbuffers rb;
  1471. memset(&rb, 0, sizeof rb);
  1472. rb.count = gspca_dev->nbufread;
  1473. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1474. rb.memory = GSPCA_MEMORY_READ;
  1475. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1476. if (ret != 0) {
  1477. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1478. return ret;
  1479. }
  1480. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1481. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1482. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1483. for (i = 0; i < gspca_dev->nbufread; i++) {
  1484. v4l2_buf.index = i;
  1485. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1486. if (ret != 0) {
  1487. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1488. return ret;
  1489. }
  1490. }
  1491. gspca_dev->memory = GSPCA_MEMORY_READ;
  1492. }
  1493. /* start streaming */
  1494. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1495. if (ret != 0)
  1496. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1497. return ret;
  1498. }
  1499. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1500. {
  1501. struct gspca_dev *gspca_dev = file->private_data;
  1502. int i, ret;
  1503. PDEBUG(D_FRAM, "poll");
  1504. poll_wait(file, &gspca_dev->wq, wait);
  1505. if (!gspca_dev->present)
  1506. return POLLERR;
  1507. /* if reqbufs is not done, the user would use read() */
  1508. if (gspca_dev->nframes == 0) {
  1509. if (gspca_dev->memory != GSPCA_MEMORY_NO)
  1510. return POLLERR; /* not the 1st time */
  1511. ret = read_alloc(gspca_dev, file);
  1512. if (ret != 0)
  1513. return POLLERR;
  1514. }
  1515. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
  1516. return POLLERR;
  1517. if (!gspca_dev->present) {
  1518. ret = POLLERR;
  1519. goto out;
  1520. }
  1521. /* check the next incoming buffer */
  1522. i = gspca_dev->fr_o;
  1523. i = gspca_dev->fr_queue[i];
  1524. if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
  1525. ret = POLLIN | POLLRDNORM; /* something to read */
  1526. else
  1527. ret = 0;
  1528. out:
  1529. mutex_unlock(&gspca_dev->queue_lock);
  1530. return ret;
  1531. }
  1532. static ssize_t dev_read(struct file *file, char __user *data,
  1533. size_t count, loff_t *ppos)
  1534. {
  1535. struct gspca_dev *gspca_dev = file->private_data;
  1536. struct gspca_frame *frame;
  1537. struct v4l2_buffer v4l2_buf;
  1538. struct timeval timestamp;
  1539. int n, ret, ret2;
  1540. PDEBUG(D_FRAM, "read (%zd)", count);
  1541. if (!gspca_dev->present)
  1542. return -ENODEV;
  1543. switch (gspca_dev->memory) {
  1544. case GSPCA_MEMORY_NO: /* first time */
  1545. ret = read_alloc(gspca_dev, file);
  1546. if (ret != 0)
  1547. return ret;
  1548. break;
  1549. case GSPCA_MEMORY_READ:
  1550. if (gspca_dev->capt_file == file)
  1551. break;
  1552. /* fall thru */
  1553. default:
  1554. return -EINVAL;
  1555. }
  1556. /* get a frame */
  1557. jiffies_to_timeval(get_jiffies_64(), &timestamp);
  1558. timestamp.tv_sec--;
  1559. n = 2;
  1560. for (;;) {
  1561. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1562. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1563. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1564. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1565. if (ret != 0) {
  1566. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1567. return ret;
  1568. }
  1569. /* if the process slept for more than 1 second,
  1570. * get a newer frame */
  1571. frame = &gspca_dev->frame[v4l2_buf.index];
  1572. if (--n < 0)
  1573. break; /* avoid infinite loop */
  1574. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1575. break;
  1576. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1577. if (ret != 0) {
  1578. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1579. return ret;
  1580. }
  1581. }
  1582. /* copy the frame */
  1583. if (count > frame->v4l2_buf.bytesused)
  1584. count = frame->v4l2_buf.bytesused;
  1585. ret = copy_to_user(data, frame->data, count);
  1586. if (ret != 0) {
  1587. PDEBUG(D_ERR|D_STREAM,
  1588. "read cp to user lack %d / %zd", ret, count);
  1589. ret = -EFAULT;
  1590. goto out;
  1591. }
  1592. ret = count;
  1593. out:
  1594. /* in each case, requeue the buffer */
  1595. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1596. if (ret2 != 0)
  1597. return ret2;
  1598. return ret;
  1599. }
  1600. static struct v4l2_file_operations dev_fops = {
  1601. .owner = THIS_MODULE,
  1602. .open = dev_open,
  1603. .release = dev_close,
  1604. .read = dev_read,
  1605. .mmap = dev_mmap,
  1606. .unlocked_ioctl = video_ioctl2,
  1607. .poll = dev_poll,
  1608. };
  1609. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1610. .vidioc_querycap = vidioc_querycap,
  1611. .vidioc_dqbuf = vidioc_dqbuf,
  1612. .vidioc_qbuf = vidioc_qbuf,
  1613. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1614. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1615. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1616. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1617. .vidioc_streamon = vidioc_streamon,
  1618. .vidioc_queryctrl = vidioc_queryctrl,
  1619. .vidioc_g_ctrl = vidioc_g_ctrl,
  1620. .vidioc_s_ctrl = vidioc_s_ctrl,
  1621. .vidioc_g_audio = vidioc_g_audio,
  1622. .vidioc_s_audio = vidioc_s_audio,
  1623. .vidioc_enumaudio = vidioc_enumaudio,
  1624. .vidioc_querymenu = vidioc_querymenu,
  1625. .vidioc_enum_input = vidioc_enum_input,
  1626. .vidioc_g_input = vidioc_g_input,
  1627. .vidioc_s_input = vidioc_s_input,
  1628. .vidioc_reqbufs = vidioc_reqbufs,
  1629. .vidioc_querybuf = vidioc_querybuf,
  1630. .vidioc_streamoff = vidioc_streamoff,
  1631. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1632. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1633. .vidioc_g_parm = vidioc_g_parm,
  1634. .vidioc_s_parm = vidioc_s_parm,
  1635. .vidioc_s_std = vidioc_s_std,
  1636. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1637. .vidiocgmbuf = vidiocgmbuf,
  1638. #endif
  1639. };
  1640. static struct video_device gspca_template = {
  1641. .name = "gspca main driver",
  1642. .fops = &dev_fops,
  1643. .ioctl_ops = &dev_ioctl_ops,
  1644. .release = gspca_release,
  1645. .minor = -1,
  1646. };
  1647. /*
  1648. * probe and create a new gspca device
  1649. *
  1650. * This function must be called by the sub-driver when it is
  1651. * called for probing a new device.
  1652. */
  1653. int gspca_dev_probe(struct usb_interface *intf,
  1654. const struct usb_device_id *id,
  1655. const struct sd_desc *sd_desc,
  1656. int dev_size,
  1657. struct module *module)
  1658. {
  1659. struct usb_interface_descriptor *interface;
  1660. struct gspca_dev *gspca_dev;
  1661. struct usb_device *dev = interface_to_usbdev(intf);
  1662. int ret;
  1663. PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
  1664. /* we don't handle multi-config cameras */
  1665. if (dev->descriptor.bNumConfigurations != 1)
  1666. return -ENODEV;
  1667. interface = &intf->cur_altsetting->desc;
  1668. if (interface->bInterfaceNumber > 0)
  1669. return -ENODEV;
  1670. /* create the device */
  1671. if (dev_size < sizeof *gspca_dev)
  1672. dev_size = sizeof *gspca_dev;
  1673. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1674. if (!gspca_dev) {
  1675. err("couldn't kzalloc gspca struct");
  1676. return -ENOMEM;
  1677. }
  1678. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1679. if (!gspca_dev->usb_buf) {
  1680. err("out of memory");
  1681. ret = -ENOMEM;
  1682. goto out;
  1683. }
  1684. gspca_dev->dev = dev;
  1685. gspca_dev->iface = interface->bInterfaceNumber;
  1686. gspca_dev->nbalt = intf->num_altsetting;
  1687. gspca_dev->sd_desc = sd_desc;
  1688. gspca_dev->nbufread = 2;
  1689. gspca_dev->empty_packet = -1; /* don't check the empty packets */
  1690. /* configure the subdriver and initialize the USB device */
  1691. ret = sd_desc->config(gspca_dev, id);
  1692. if (ret < 0)
  1693. goto out;
  1694. ret = sd_desc->init(gspca_dev);
  1695. if (ret < 0)
  1696. goto out;
  1697. ret = gspca_set_alt0(gspca_dev);
  1698. if (ret < 0)
  1699. goto out;
  1700. gspca_set_default_mode(gspca_dev);
  1701. mutex_init(&gspca_dev->usb_lock);
  1702. mutex_init(&gspca_dev->read_lock);
  1703. mutex_init(&gspca_dev->queue_lock);
  1704. init_waitqueue_head(&gspca_dev->wq);
  1705. /* init video stuff */
  1706. memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
  1707. gspca_dev->vdev.parent = &dev->dev;
  1708. gspca_dev->module = module;
  1709. gspca_dev->present = 1;
  1710. ret = video_register_device(&gspca_dev->vdev,
  1711. VFL_TYPE_GRABBER,
  1712. video_nr);
  1713. if (ret < 0) {
  1714. err("video_register_device err %d", ret);
  1715. goto out;
  1716. }
  1717. usb_set_intfdata(intf, gspca_dev);
  1718. PDEBUG(D_PROBE, "probe ok");
  1719. return 0;
  1720. out:
  1721. kfree(gspca_dev->usb_buf);
  1722. kfree(gspca_dev);
  1723. return ret;
  1724. }
  1725. EXPORT_SYMBOL(gspca_dev_probe);
  1726. /*
  1727. * USB disconnection
  1728. *
  1729. * This function must be called by the sub-driver
  1730. * when the device disconnects, after the specific resources are freed.
  1731. */
  1732. void gspca_disconnect(struct usb_interface *intf)
  1733. {
  1734. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1735. mutex_lock(&gspca_dev->usb_lock);
  1736. gspca_dev->present = 0;
  1737. mutex_unlock(&gspca_dev->usb_lock);
  1738. destroy_urbs(gspca_dev);
  1739. gspca_dev->dev = NULL;
  1740. usb_set_intfdata(intf, NULL);
  1741. /* release the device */
  1742. /* (this will call gspca_release() immediatly or on last close) */
  1743. video_unregister_device(&gspca_dev->vdev);
  1744. PDEBUG(D_PROBE, "disconnect complete");
  1745. }
  1746. EXPORT_SYMBOL(gspca_disconnect);
  1747. #ifdef CONFIG_PM
  1748. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  1749. {
  1750. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1751. if (!gspca_dev->streaming)
  1752. return 0;
  1753. gspca_dev->frozen = 1; /* avoid urb error messages */
  1754. if (gspca_dev->sd_desc->stopN)
  1755. gspca_dev->sd_desc->stopN(gspca_dev);
  1756. destroy_urbs(gspca_dev);
  1757. gspca_set_alt0(gspca_dev);
  1758. if (gspca_dev->sd_desc->stop0)
  1759. gspca_dev->sd_desc->stop0(gspca_dev);
  1760. return 0;
  1761. }
  1762. EXPORT_SYMBOL(gspca_suspend);
  1763. int gspca_resume(struct usb_interface *intf)
  1764. {
  1765. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1766. gspca_dev->frozen = 0;
  1767. gspca_dev->sd_desc->init(gspca_dev);
  1768. if (gspca_dev->streaming)
  1769. return gspca_init_transfer(gspca_dev);
  1770. return 0;
  1771. }
  1772. EXPORT_SYMBOL(gspca_resume);
  1773. #endif
  1774. /* -- cam driver utility functions -- */
  1775. /* auto gain and exposure algorithm based on the knee algorithm described here:
  1776. http://ytse.tricolour.net/docs/LowLightOptimization.html
  1777. Returns 0 if no changes were made, 1 if the gain and or exposure settings
  1778. where changed. */
  1779. int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
  1780. int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
  1781. {
  1782. int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
  1783. const struct ctrl *gain_ctrl = NULL;
  1784. const struct ctrl *exposure_ctrl = NULL;
  1785. const struct ctrl *autogain_ctrl = NULL;
  1786. int retval = 0;
  1787. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  1788. if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
  1789. gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
  1790. if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
  1791. exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
  1792. if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
  1793. autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
  1794. }
  1795. if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
  1796. PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
  1797. "on cam without (auto)gain/exposure");
  1798. return 0;
  1799. }
  1800. if (gain_ctrl->get(gspca_dev, &gain) ||
  1801. exposure_ctrl->get(gspca_dev, &exposure) ||
  1802. autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
  1803. return 0;
  1804. orig_gain = gain;
  1805. orig_exposure = exposure;
  1806. /* If we are of a multiple of deadzone, do multiple steps to reach the
  1807. desired lumination fast (with the risc of a slight overshoot) */
  1808. steps = abs(desired_avg_lum - avg_lum) / deadzone;
  1809. PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
  1810. avg_lum, desired_avg_lum, steps);
  1811. for (i = 0; i < steps; i++) {
  1812. if (avg_lum > desired_avg_lum) {
  1813. if (gain > gain_knee)
  1814. gain--;
  1815. else if (exposure > exposure_knee)
  1816. exposure--;
  1817. else if (gain > gain_ctrl->qctrl.default_value)
  1818. gain--;
  1819. else if (exposure > exposure_ctrl->qctrl.minimum)
  1820. exposure--;
  1821. else if (gain > gain_ctrl->qctrl.minimum)
  1822. gain--;
  1823. else
  1824. break;
  1825. } else {
  1826. if (gain < gain_ctrl->qctrl.default_value)
  1827. gain++;
  1828. else if (exposure < exposure_knee)
  1829. exposure++;
  1830. else if (gain < gain_knee)
  1831. gain++;
  1832. else if (exposure < exposure_ctrl->qctrl.maximum)
  1833. exposure++;
  1834. else if (gain < gain_ctrl->qctrl.maximum)
  1835. gain++;
  1836. else
  1837. break;
  1838. }
  1839. }
  1840. if (gain != orig_gain) {
  1841. gain_ctrl->set(gspca_dev, gain);
  1842. retval = 1;
  1843. }
  1844. if (exposure != orig_exposure) {
  1845. exposure_ctrl->set(gspca_dev, exposure);
  1846. retval = 1;
  1847. }
  1848. return retval;
  1849. }
  1850. EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
  1851. /* -- module insert / remove -- */
  1852. static int __init gspca_init(void)
  1853. {
  1854. info("main v%d.%d.%d registered",
  1855. (DRIVER_VERSION_NUMBER >> 16) & 0xff,
  1856. (DRIVER_VERSION_NUMBER >> 8) & 0xff,
  1857. DRIVER_VERSION_NUMBER & 0xff);
  1858. return 0;
  1859. }
  1860. static void __exit gspca_exit(void)
  1861. {
  1862. info("main deregistered");
  1863. }
  1864. module_init(gspca_init);
  1865. module_exit(gspca_exit);
  1866. #ifdef GSPCA_DEBUG
  1867. module_param_named(debug, gspca_debug, int, 0644);
  1868. MODULE_PARM_DESC(debug,
  1869. "Debug (bit) 0x01:error 0x02:probe 0x04:config"
  1870. " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
  1871. " 0x0100: v4l2");
  1872. #endif