gspca.c 50 KB

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