gspca.c 51 KB

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