gspca.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459
  1. /*
  2. * Main USB camera driver
  3. *
  4. * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
  5. *
  6. * Camera button input handling by Márton Németh
  7. * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #define GSPCA_VERSION "2.14.0"
  25. #include <linux/init.h>
  26. #include <linux/fs.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/mm.h>
  31. #include <linux/string.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/io.h>
  34. #include <asm/page.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/ktime.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <media/v4l2-ctrls.h>
  39. #include <media/v4l2-fh.h>
  40. #include <media/v4l2-event.h>
  41. #include "gspca.h"
  42. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  43. #include <linux/input.h>
  44. #include <linux/usb/input.h>
  45. #endif
  46. /* global values */
  47. #define DEF_NURBS 3 /* default number of URBs */
  48. #if DEF_NURBS > MAX_NURBS
  49. #error "DEF_NURBS too big"
  50. #endif
  51. MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
  52. MODULE_DESCRIPTION("GSPCA USB Camera Driver");
  53. MODULE_LICENSE("GPL");
  54. MODULE_VERSION(GSPCA_VERSION);
  55. #ifdef GSPCA_DEBUG
  56. int gspca_debug = D_ERR | D_PROBE;
  57. EXPORT_SYMBOL(gspca_debug);
  58. static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
  59. {
  60. if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
  61. PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
  62. txt,
  63. pixfmt & 0xff,
  64. (pixfmt >> 8) & 0xff,
  65. (pixfmt >> 16) & 0xff,
  66. pixfmt >> 24,
  67. w, h);
  68. } else {
  69. PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
  70. txt,
  71. pixfmt,
  72. w, h);
  73. }
  74. }
  75. #else
  76. #define PDEBUG_MODE(txt, pixfmt, w, h)
  77. #endif
  78. /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
  79. #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
  80. #define GSPCA_MEMORY_READ 7
  81. #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
  82. /*
  83. * VMA operations.
  84. */
  85. static void gspca_vm_open(struct vm_area_struct *vma)
  86. {
  87. struct gspca_frame *frame = vma->vm_private_data;
  88. frame->vma_use_count++;
  89. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
  90. }
  91. static void gspca_vm_close(struct vm_area_struct *vma)
  92. {
  93. struct gspca_frame *frame = vma->vm_private_data;
  94. if (--frame->vma_use_count <= 0)
  95. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  96. }
  97. static const struct vm_operations_struct gspca_vm_ops = {
  98. .open = gspca_vm_open,
  99. .close = gspca_vm_close,
  100. };
  101. /*
  102. * Input and interrupt endpoint handling functions
  103. */
  104. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  105. static void int_irq(struct urb *urb)
  106. {
  107. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  108. int ret;
  109. ret = urb->status;
  110. switch (ret) {
  111. case 0:
  112. if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
  113. urb->transfer_buffer, urb->actual_length) < 0) {
  114. PDEBUG(D_ERR, "Unknown packet received");
  115. }
  116. break;
  117. case -ENOENT:
  118. case -ECONNRESET:
  119. case -ENODEV:
  120. case -ESHUTDOWN:
  121. /* Stop is requested either by software or hardware is gone,
  122. * keep the ret value non-zero and don't resubmit later.
  123. */
  124. break;
  125. default:
  126. PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
  127. urb->status = 0;
  128. ret = 0;
  129. }
  130. if (ret == 0) {
  131. ret = usb_submit_urb(urb, GFP_ATOMIC);
  132. if (ret < 0)
  133. pr_err("Resubmit URB failed with error %i\n", ret);
  134. }
  135. }
  136. static int gspca_input_connect(struct gspca_dev *dev)
  137. {
  138. struct input_dev *input_dev;
  139. int err = 0;
  140. dev->input_dev = NULL;
  141. if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
  142. input_dev = input_allocate_device();
  143. if (!input_dev)
  144. return -ENOMEM;
  145. usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
  146. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  147. input_dev->name = dev->sd_desc->name;
  148. input_dev->phys = dev->phys;
  149. usb_to_input_id(dev->dev, &input_dev->id);
  150. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  151. input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
  152. input_dev->dev.parent = &dev->dev->dev;
  153. err = input_register_device(input_dev);
  154. if (err) {
  155. pr_err("Input device registration failed with error %i\n",
  156. err);
  157. input_dev->dev.parent = NULL;
  158. input_free_device(input_dev);
  159. } else {
  160. dev->input_dev = input_dev;
  161. }
  162. }
  163. return err;
  164. }
  165. static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
  166. struct usb_endpoint_descriptor *ep)
  167. {
  168. unsigned int buffer_len;
  169. int interval;
  170. struct urb *urb;
  171. struct usb_device *dev;
  172. void *buffer = NULL;
  173. int ret = -EINVAL;
  174. buffer_len = le16_to_cpu(ep->wMaxPacketSize);
  175. interval = ep->bInterval;
  176. PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
  177. "buffer_len=%u, interval=%u",
  178. ep->bEndpointAddress, buffer_len, interval);
  179. dev = gspca_dev->dev;
  180. urb = usb_alloc_urb(0, GFP_KERNEL);
  181. if (!urb) {
  182. ret = -ENOMEM;
  183. goto error;
  184. }
  185. buffer = usb_alloc_coherent(dev, buffer_len,
  186. GFP_KERNEL, &urb->transfer_dma);
  187. if (!buffer) {
  188. ret = -ENOMEM;
  189. goto error_buffer;
  190. }
  191. usb_fill_int_urb(urb, dev,
  192. usb_rcvintpipe(dev, ep->bEndpointAddress),
  193. buffer, buffer_len,
  194. int_irq, (void *)gspca_dev, interval);
  195. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  196. ret = usb_submit_urb(urb, GFP_KERNEL);
  197. if (ret < 0) {
  198. PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
  199. goto error_submit;
  200. }
  201. gspca_dev->int_urb = urb;
  202. return ret;
  203. error_submit:
  204. usb_free_coherent(dev,
  205. urb->transfer_buffer_length,
  206. urb->transfer_buffer,
  207. urb->transfer_dma);
  208. error_buffer:
  209. usb_free_urb(urb);
  210. error:
  211. return ret;
  212. }
  213. static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
  214. {
  215. struct usb_interface *intf;
  216. struct usb_host_interface *intf_desc;
  217. struct usb_endpoint_descriptor *ep;
  218. int i;
  219. if (gspca_dev->sd_desc->int_pkt_scan) {
  220. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  221. intf_desc = intf->cur_altsetting;
  222. for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
  223. ep = &intf_desc->endpoint[i].desc;
  224. if (usb_endpoint_dir_in(ep) &&
  225. usb_endpoint_xfer_int(ep)) {
  226. alloc_and_submit_int_urb(gspca_dev, ep);
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
  233. {
  234. struct urb *urb;
  235. urb = gspca_dev->int_urb;
  236. if (urb) {
  237. gspca_dev->int_urb = NULL;
  238. usb_kill_urb(urb);
  239. usb_free_coherent(gspca_dev->dev,
  240. urb->transfer_buffer_length,
  241. urb->transfer_buffer,
  242. urb->transfer_dma);
  243. usb_free_urb(urb);
  244. }
  245. }
  246. #else
  247. static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
  248. {
  249. }
  250. static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
  251. {
  252. }
  253. static inline int gspca_input_connect(struct gspca_dev *dev)
  254. {
  255. return 0;
  256. }
  257. #endif
  258. /*
  259. * fill a video frame from an URB and resubmit
  260. */
  261. static void fill_frame(struct gspca_dev *gspca_dev,
  262. struct urb *urb)
  263. {
  264. u8 *data; /* address of data in the iso message */
  265. int i, len, st;
  266. cam_pkt_op pkt_scan;
  267. if (urb->status != 0) {
  268. if (urb->status == -ESHUTDOWN)
  269. return; /* disconnection */
  270. #ifdef CONFIG_PM
  271. if (gspca_dev->frozen)
  272. return;
  273. #endif
  274. PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
  275. urb->status = 0;
  276. goto resubmit;
  277. }
  278. pkt_scan = gspca_dev->sd_desc->pkt_scan;
  279. for (i = 0; i < urb->number_of_packets; i++) {
  280. len = urb->iso_frame_desc[i].actual_length;
  281. /* check the packet status and length */
  282. st = urb->iso_frame_desc[i].status;
  283. if (st) {
  284. pr_err("ISOC data error: [%d] len=%d, status=%d\n",
  285. i, len, st);
  286. gspca_dev->last_packet_type = DISCARD_PACKET;
  287. continue;
  288. }
  289. if (len == 0) {
  290. if (gspca_dev->empty_packet == 0)
  291. gspca_dev->empty_packet = 1;
  292. continue;
  293. }
  294. /* let the packet be analyzed by the subdriver */
  295. PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
  296. i, urb->iso_frame_desc[i].offset, len);
  297. data = (u8 *) urb->transfer_buffer
  298. + urb->iso_frame_desc[i].offset;
  299. pkt_scan(gspca_dev, data, len);
  300. }
  301. resubmit:
  302. /* resubmit the URB */
  303. st = usb_submit_urb(urb, GFP_ATOMIC);
  304. if (st < 0)
  305. pr_err("usb_submit_urb() ret %d\n", st);
  306. }
  307. /*
  308. * ISOC message interrupt from the USB device
  309. *
  310. * Analyse each packet and call the subdriver for copy to the frame buffer.
  311. */
  312. static void isoc_irq(struct urb *urb)
  313. {
  314. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  315. PDEBUG(D_PACK, "isoc irq");
  316. if (!gspca_dev->streaming)
  317. return;
  318. fill_frame(gspca_dev, urb);
  319. }
  320. /*
  321. * bulk message interrupt from the USB device
  322. */
  323. static void bulk_irq(struct urb *urb)
  324. {
  325. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  326. int st;
  327. PDEBUG(D_PACK, "bulk irq");
  328. if (!gspca_dev->streaming)
  329. return;
  330. switch (urb->status) {
  331. case 0:
  332. break;
  333. case -ESHUTDOWN:
  334. return; /* disconnection */
  335. default:
  336. #ifdef CONFIG_PM
  337. if (gspca_dev->frozen)
  338. return;
  339. #endif
  340. PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
  341. urb->status = 0;
  342. goto resubmit;
  343. }
  344. PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
  345. gspca_dev->sd_desc->pkt_scan(gspca_dev,
  346. urb->transfer_buffer,
  347. urb->actual_length);
  348. resubmit:
  349. /* resubmit the URB */
  350. if (gspca_dev->cam.bulk_nurbs != 0) {
  351. st = usb_submit_urb(urb, GFP_ATOMIC);
  352. if (st < 0)
  353. pr_err("usb_submit_urb() ret %d\n", st);
  354. }
  355. }
  356. /*
  357. * add data to the current frame
  358. *
  359. * This function is called by the subdrivers at interrupt level.
  360. *
  361. * To build a frame, these ones must add
  362. * - one FIRST_PACKET
  363. * - 0 or many INTER_PACKETs
  364. * - one LAST_PACKET
  365. * DISCARD_PACKET invalidates the whole frame.
  366. */
  367. void gspca_frame_add(struct gspca_dev *gspca_dev,
  368. enum gspca_packet_type packet_type,
  369. const u8 *data,
  370. int len)
  371. {
  372. struct gspca_frame *frame;
  373. int i, j;
  374. PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
  375. if (packet_type == FIRST_PACKET) {
  376. i = atomic_read(&gspca_dev->fr_i);
  377. /* if there are no queued buffer, discard the whole frame */
  378. if (i == atomic_read(&gspca_dev->fr_q)) {
  379. gspca_dev->last_packet_type = DISCARD_PACKET;
  380. gspca_dev->sequence++;
  381. return;
  382. }
  383. j = gspca_dev->fr_queue[i];
  384. frame = &gspca_dev->frame[j];
  385. frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
  386. frame->v4l2_buf.sequence = gspca_dev->sequence++;
  387. gspca_dev->image = frame->data;
  388. gspca_dev->image_len = 0;
  389. } else {
  390. switch (gspca_dev->last_packet_type) {
  391. case DISCARD_PACKET:
  392. if (packet_type == LAST_PACKET) {
  393. gspca_dev->last_packet_type = packet_type;
  394. gspca_dev->image = NULL;
  395. gspca_dev->image_len = 0;
  396. }
  397. return;
  398. case LAST_PACKET:
  399. return;
  400. }
  401. }
  402. /* append the packet to the frame buffer */
  403. if (len > 0) {
  404. if (gspca_dev->image_len + len > gspca_dev->frsz) {
  405. PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
  406. gspca_dev->image_len + len,
  407. gspca_dev->frsz);
  408. packet_type = DISCARD_PACKET;
  409. } else {
  410. /* !! image is NULL only when last pkt is LAST or DISCARD
  411. if (gspca_dev->image == NULL) {
  412. pr_err("gspca_frame_add() image == NULL\n");
  413. return;
  414. }
  415. */
  416. memcpy(gspca_dev->image + gspca_dev->image_len,
  417. data, len);
  418. gspca_dev->image_len += len;
  419. }
  420. }
  421. gspca_dev->last_packet_type = packet_type;
  422. /* if last packet, invalidate packet concatenation until
  423. * next first packet, wake up the application and advance
  424. * in the queue */
  425. if (packet_type == LAST_PACKET) {
  426. i = atomic_read(&gspca_dev->fr_i);
  427. j = gspca_dev->fr_queue[i];
  428. frame = &gspca_dev->frame[j];
  429. frame->v4l2_buf.bytesused = gspca_dev->image_len;
  430. frame->v4l2_buf.flags = (frame->v4l2_buf.flags
  431. | V4L2_BUF_FLAG_DONE)
  432. & ~V4L2_BUF_FLAG_QUEUED;
  433. i = (i + 1) % GSPCA_MAX_FRAMES;
  434. atomic_set(&gspca_dev->fr_i, i);
  435. wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
  436. PDEBUG(D_FRAM, "frame complete len:%d",
  437. frame->v4l2_buf.bytesused);
  438. gspca_dev->image = NULL;
  439. gspca_dev->image_len = 0;
  440. }
  441. }
  442. EXPORT_SYMBOL(gspca_frame_add);
  443. static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
  444. enum v4l2_memory memory, unsigned int count)
  445. {
  446. struct gspca_frame *frame;
  447. unsigned int frsz;
  448. int i;
  449. i = gspca_dev->curr_mode;
  450. frsz = gspca_dev->cam.cam_mode[i].sizeimage;
  451. PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
  452. frsz = PAGE_ALIGN(frsz);
  453. if (count >= GSPCA_MAX_FRAMES)
  454. count = GSPCA_MAX_FRAMES - 1;
  455. gspca_dev->frbuf = vmalloc_32(frsz * count);
  456. if (!gspca_dev->frbuf) {
  457. pr_err("frame alloc failed\n");
  458. return -ENOMEM;
  459. }
  460. gspca_dev->capt_file = file;
  461. gspca_dev->memory = memory;
  462. gspca_dev->frsz = frsz;
  463. gspca_dev->nframes = count;
  464. for (i = 0; i < count; i++) {
  465. frame = &gspca_dev->frame[i];
  466. frame->v4l2_buf.index = i;
  467. frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  468. frame->v4l2_buf.flags = 0;
  469. frame->v4l2_buf.field = V4L2_FIELD_NONE;
  470. frame->v4l2_buf.length = frsz;
  471. frame->v4l2_buf.memory = memory;
  472. frame->v4l2_buf.sequence = 0;
  473. frame->data = gspca_dev->frbuf + i * frsz;
  474. frame->v4l2_buf.m.offset = i * frsz;
  475. }
  476. atomic_set(&gspca_dev->fr_q, 0);
  477. atomic_set(&gspca_dev->fr_i, 0);
  478. gspca_dev->fr_o = 0;
  479. return 0;
  480. }
  481. static void frame_free(struct gspca_dev *gspca_dev)
  482. {
  483. int i;
  484. PDEBUG(D_STREAM, "frame free");
  485. if (gspca_dev->frbuf != NULL) {
  486. vfree(gspca_dev->frbuf);
  487. gspca_dev->frbuf = NULL;
  488. for (i = 0; i < gspca_dev->nframes; i++)
  489. gspca_dev->frame[i].data = NULL;
  490. }
  491. gspca_dev->nframes = 0;
  492. gspca_dev->frsz = 0;
  493. gspca_dev->capt_file = NULL;
  494. gspca_dev->memory = GSPCA_MEMORY_NO;
  495. }
  496. static void destroy_urbs(struct gspca_dev *gspca_dev)
  497. {
  498. struct urb *urb;
  499. unsigned int i;
  500. PDEBUG(D_STREAM, "kill transfer");
  501. for (i = 0; i < MAX_NURBS; i++) {
  502. urb = gspca_dev->urb[i];
  503. if (urb == NULL)
  504. break;
  505. gspca_dev->urb[i] = NULL;
  506. usb_kill_urb(urb);
  507. if (urb->transfer_buffer != NULL)
  508. usb_free_coherent(gspca_dev->dev,
  509. urb->transfer_buffer_length,
  510. urb->transfer_buffer,
  511. urb->transfer_dma);
  512. usb_free_urb(urb);
  513. }
  514. }
  515. static int gspca_set_alt0(struct gspca_dev *gspca_dev)
  516. {
  517. int ret;
  518. if (gspca_dev->alt == 0)
  519. return 0;
  520. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
  521. if (ret < 0)
  522. pr_err("set alt 0 err %d\n", ret);
  523. return ret;
  524. }
  525. /* Note: both the queue and the usb locks should be held when calling this */
  526. static void gspca_stream_off(struct gspca_dev *gspca_dev)
  527. {
  528. gspca_dev->streaming = 0;
  529. gspca_dev->usb_err = 0;
  530. if (gspca_dev->sd_desc->stopN)
  531. gspca_dev->sd_desc->stopN(gspca_dev);
  532. destroy_urbs(gspca_dev);
  533. gspca_input_destroy_urb(gspca_dev);
  534. gspca_set_alt0(gspca_dev);
  535. gspca_input_create_urb(gspca_dev);
  536. if (gspca_dev->sd_desc->stop0)
  537. gspca_dev->sd_desc->stop0(gspca_dev);
  538. PDEBUG(D_STREAM, "stream off OK");
  539. }
  540. /*
  541. * look for an input transfer endpoint in an alternate setting
  542. */
  543. static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
  544. int xfer)
  545. {
  546. struct usb_host_endpoint *ep;
  547. int i, attr;
  548. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  549. ep = &alt->endpoint[i];
  550. attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  551. if (attr == xfer
  552. && ep->desc.wMaxPacketSize != 0
  553. && usb_endpoint_dir_in(&ep->desc))
  554. return ep;
  555. }
  556. return NULL;
  557. }
  558. /* compute the minimum bandwidth for the current transfer */
  559. static u32 which_bandwidth(struct gspca_dev *gspca_dev)
  560. {
  561. u32 bandwidth;
  562. int i;
  563. /* get the (max) image size */
  564. i = gspca_dev->curr_mode;
  565. bandwidth = gspca_dev->cam.cam_mode[i].sizeimage;
  566. /* if the image is compressed, estimate its mean size */
  567. if (!gspca_dev->cam.needs_full_bandwidth &&
  568. bandwidth < gspca_dev->cam.cam_mode[i].width *
  569. gspca_dev->cam.cam_mode[i].height)
  570. bandwidth = bandwidth * 3 / 8; /* 0.375 */
  571. /* estimate the frame rate */
  572. if (gspca_dev->sd_desc->get_streamparm) {
  573. struct v4l2_streamparm parm;
  574. gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
  575. bandwidth *= parm.parm.capture.timeperframe.denominator;
  576. bandwidth /= parm.parm.capture.timeperframe.numerator;
  577. } else {
  578. /* don't hope more than 15 fps with USB 1.1 and
  579. * image resolution >= 640x480 */
  580. if (gspca_dev->width >= 640
  581. && gspca_dev->dev->speed == USB_SPEED_FULL)
  582. bandwidth *= 15; /* 15 fps */
  583. else
  584. bandwidth *= 30; /* 30 fps */
  585. }
  586. PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
  587. return bandwidth;
  588. }
  589. /* endpoint table */
  590. #define MAX_ALT 16
  591. struct ep_tb_s {
  592. u32 alt;
  593. u32 bandwidth;
  594. };
  595. /*
  596. * build the table of the endpoints
  597. * and compute the minimum bandwidth for the image transfer
  598. */
  599. static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
  600. struct usb_interface *intf,
  601. struct ep_tb_s *ep_tb)
  602. {
  603. struct usb_host_endpoint *ep;
  604. int i, j, nbalt, psize, found;
  605. u32 bandwidth, last_bw;
  606. nbalt = intf->num_altsetting;
  607. if (nbalt > MAX_ALT)
  608. nbalt = MAX_ALT; /* fixme: should warn */
  609. /* build the endpoint table */
  610. i = 0;
  611. last_bw = 0;
  612. for (;;) {
  613. ep_tb->bandwidth = 2000 * 2000 * 120;
  614. found = 0;
  615. for (j = 0; j < nbalt; j++) {
  616. ep = alt_xfer(&intf->altsetting[j],
  617. USB_ENDPOINT_XFER_ISOC);
  618. if (ep == NULL)
  619. continue;
  620. if (ep->desc.bInterval == 0) {
  621. pr_err("alt %d iso endp with 0 interval\n", j);
  622. continue;
  623. }
  624. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  625. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  626. bandwidth = psize * 1000;
  627. if (gspca_dev->dev->speed == USB_SPEED_HIGH
  628. || gspca_dev->dev->speed == USB_SPEED_SUPER)
  629. bandwidth *= 8;
  630. bandwidth /= 1 << (ep->desc.bInterval - 1);
  631. if (bandwidth <= last_bw)
  632. continue;
  633. if (bandwidth < ep_tb->bandwidth) {
  634. ep_tb->bandwidth = bandwidth;
  635. ep_tb->alt = j;
  636. found = 1;
  637. }
  638. }
  639. if (!found)
  640. break;
  641. PDEBUG(D_STREAM, "alt %d bandwidth %d",
  642. ep_tb->alt, ep_tb->bandwidth);
  643. last_bw = ep_tb->bandwidth;
  644. i++;
  645. ep_tb++;
  646. }
  647. /*
  648. * If the camera:
  649. * has a usb audio class interface (a built in usb mic); and
  650. * is a usb 1 full speed device; and
  651. * uses the max full speed iso bandwidth; and
  652. * and has more than 1 alt setting
  653. * then skip the highest alt setting to spare bandwidth for the mic
  654. */
  655. if (gspca_dev->audio &&
  656. gspca_dev->dev->speed == USB_SPEED_FULL &&
  657. last_bw >= 1000000 &&
  658. i > 1) {
  659. PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
  660. i--;
  661. ep_tb--;
  662. }
  663. /* get the requested bandwidth and start at the highest atlsetting */
  664. bandwidth = which_bandwidth(gspca_dev);
  665. ep_tb--;
  666. while (i > 1) {
  667. ep_tb--;
  668. if (ep_tb->bandwidth < bandwidth)
  669. break;
  670. i--;
  671. }
  672. return i;
  673. }
  674. /*
  675. * create the URBs for image transfer
  676. */
  677. static int create_urbs(struct gspca_dev *gspca_dev,
  678. struct usb_host_endpoint *ep)
  679. {
  680. struct urb *urb;
  681. int n, nurbs, i, psize, npkt, bsize;
  682. /* calculate the packet size and the number of packets */
  683. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  684. if (!gspca_dev->cam.bulk) { /* isoc */
  685. /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
  686. if (gspca_dev->pkt_size == 0)
  687. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  688. else
  689. psize = gspca_dev->pkt_size;
  690. npkt = gspca_dev->cam.npkt;
  691. if (npkt == 0)
  692. npkt = 32; /* default value */
  693. bsize = psize * npkt;
  694. PDEBUG(D_STREAM,
  695. "isoc %d pkts size %d = bsize:%d",
  696. npkt, psize, bsize);
  697. nurbs = DEF_NURBS;
  698. } else { /* bulk */
  699. npkt = 0;
  700. bsize = gspca_dev->cam.bulk_size;
  701. if (bsize == 0)
  702. bsize = psize;
  703. PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
  704. if (gspca_dev->cam.bulk_nurbs != 0)
  705. nurbs = gspca_dev->cam.bulk_nurbs;
  706. else
  707. nurbs = 1;
  708. }
  709. for (n = 0; n < nurbs; n++) {
  710. urb = usb_alloc_urb(npkt, GFP_KERNEL);
  711. if (!urb) {
  712. pr_err("usb_alloc_urb failed\n");
  713. return -ENOMEM;
  714. }
  715. gspca_dev->urb[n] = urb;
  716. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  717. bsize,
  718. GFP_KERNEL,
  719. &urb->transfer_dma);
  720. if (urb->transfer_buffer == NULL) {
  721. pr_err("usb_alloc_coherent failed\n");
  722. return -ENOMEM;
  723. }
  724. urb->dev = gspca_dev->dev;
  725. urb->context = gspca_dev;
  726. urb->transfer_buffer_length = bsize;
  727. if (npkt != 0) { /* ISOC */
  728. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  729. ep->desc.bEndpointAddress);
  730. urb->transfer_flags = URB_ISO_ASAP
  731. | URB_NO_TRANSFER_DMA_MAP;
  732. urb->interval = 1 << (ep->desc.bInterval - 1);
  733. urb->complete = isoc_irq;
  734. urb->number_of_packets = npkt;
  735. for (i = 0; i < npkt; i++) {
  736. urb->iso_frame_desc[i].length = psize;
  737. urb->iso_frame_desc[i].offset = psize * i;
  738. }
  739. } else { /* bulk */
  740. urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
  741. ep->desc.bEndpointAddress);
  742. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  743. urb->complete = bulk_irq;
  744. }
  745. }
  746. return 0;
  747. }
  748. /*
  749. * start the USB transfer
  750. */
  751. static int gspca_init_transfer(struct gspca_dev *gspca_dev)
  752. {
  753. struct usb_interface *intf;
  754. struct usb_host_endpoint *ep;
  755. struct urb *urb;
  756. struct ep_tb_s ep_tb[MAX_ALT];
  757. int n, ret, xfer, alt, alt_idx;
  758. /* reset the streaming variables */
  759. gspca_dev->image = NULL;
  760. gspca_dev->image_len = 0;
  761. gspca_dev->last_packet_type = DISCARD_PACKET;
  762. gspca_dev->sequence = 0;
  763. gspca_dev->usb_err = 0;
  764. /* do the specific subdriver stuff before endpoint selection */
  765. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  766. gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
  767. if (gspca_dev->sd_desc->isoc_init) {
  768. ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
  769. if (ret < 0)
  770. return ret;
  771. }
  772. xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
  773. : USB_ENDPOINT_XFER_ISOC;
  774. /* if bulk or the subdriver forced an altsetting, get the endpoint */
  775. if (gspca_dev->alt != 0) {
  776. gspca_dev->alt--; /* (previous version compatibility) */
  777. ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer);
  778. if (ep == NULL) {
  779. pr_err("bad altsetting %d\n", gspca_dev->alt);
  780. return -EIO;
  781. }
  782. ep_tb[0].alt = gspca_dev->alt;
  783. alt_idx = 1;
  784. } else {
  785. /* else, compute the minimum bandwidth
  786. * and build the endpoint table */
  787. alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
  788. if (alt_idx <= 0) {
  789. pr_err("no transfer endpoint found\n");
  790. return -EIO;
  791. }
  792. }
  793. /* set the highest alternate setting and
  794. * loop until urb submit succeeds */
  795. gspca_input_destroy_urb(gspca_dev);
  796. gspca_dev->alt = ep_tb[--alt_idx].alt;
  797. alt = -1;
  798. for (;;) {
  799. if (alt != gspca_dev->alt) {
  800. alt = gspca_dev->alt;
  801. if (intf->num_altsetting > 1) {
  802. ret = usb_set_interface(gspca_dev->dev,
  803. gspca_dev->iface,
  804. alt);
  805. if (ret < 0) {
  806. if (ret == -ENOSPC)
  807. goto retry; /*fixme: ugly*/
  808. pr_err("set alt %d err %d\n", alt, ret);
  809. goto out;
  810. }
  811. }
  812. }
  813. if (!gspca_dev->cam.no_urb_create) {
  814. PDEBUG(D_STREAM, "init transfer alt %d", alt);
  815. ret = create_urbs(gspca_dev,
  816. alt_xfer(&intf->altsetting[alt], xfer));
  817. if (ret < 0) {
  818. destroy_urbs(gspca_dev);
  819. goto out;
  820. }
  821. }
  822. /* clear the bulk endpoint */
  823. if (gspca_dev->cam.bulk)
  824. usb_clear_halt(gspca_dev->dev,
  825. gspca_dev->urb[0]->pipe);
  826. /* start the cam */
  827. ret = gspca_dev->sd_desc->start(gspca_dev);
  828. if (ret < 0) {
  829. destroy_urbs(gspca_dev);
  830. goto out;
  831. }
  832. gspca_dev->streaming = 1;
  833. /* some bulk transfers are started by the subdriver */
  834. if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
  835. break;
  836. /* submit the URBs */
  837. for (n = 0; n < MAX_NURBS; n++) {
  838. urb = gspca_dev->urb[n];
  839. if (urb == NULL)
  840. break;
  841. ret = usb_submit_urb(urb, GFP_KERNEL);
  842. if (ret < 0)
  843. break;
  844. }
  845. if (ret >= 0)
  846. break; /* transfer is started */
  847. /* something when wrong
  848. * stop the webcam and free the transfer resources */
  849. gspca_stream_off(gspca_dev);
  850. if (ret != -ENOSPC) {
  851. pr_err("usb_submit_urb alt %d err %d\n",
  852. gspca_dev->alt, ret);
  853. goto out;
  854. }
  855. /* the bandwidth is not wide enough
  856. * negotiate or try a lower alternate setting */
  857. retry:
  858. PDEBUG(D_ERR|D_STREAM,
  859. "alt %d - bandwidth not wide enough - trying again",
  860. alt);
  861. msleep(20); /* wait for kill complete */
  862. if (gspca_dev->sd_desc->isoc_nego) {
  863. ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
  864. if (ret < 0)
  865. goto out;
  866. } else {
  867. if (alt_idx <= 0) {
  868. pr_err("no transfer endpoint found\n");
  869. ret = -EIO;
  870. goto out;
  871. }
  872. gspca_dev->alt = ep_tb[--alt_idx].alt;
  873. }
  874. }
  875. out:
  876. gspca_input_create_urb(gspca_dev);
  877. return ret;
  878. }
  879. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  880. {
  881. struct gspca_ctrl *ctrl;
  882. int i;
  883. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  884. gspca_dev->curr_mode = i;
  885. gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
  886. gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
  887. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
  888. /* set the current control values to their default values
  889. * which may have changed in sd_init() */
  890. /* does nothing if ctrl_handler == NULL */
  891. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  892. ctrl = gspca_dev->cam.ctrls;
  893. if (ctrl != NULL) {
  894. for (i = 0;
  895. i < gspca_dev->sd_desc->nctrls;
  896. i++, ctrl++)
  897. ctrl->val = ctrl->def;
  898. }
  899. }
  900. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  901. int width, int height)
  902. {
  903. int i;
  904. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  905. if (width >= gspca_dev->cam.cam_mode[i].width
  906. && height >= gspca_dev->cam.cam_mode[i].height)
  907. break;
  908. }
  909. return i;
  910. }
  911. /*
  912. * search a mode with the right pixel format
  913. */
  914. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  915. int mode,
  916. int pixfmt)
  917. {
  918. int modeU, modeD;
  919. modeU = modeD = mode;
  920. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  921. if (--modeD >= 0) {
  922. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  923. == pixfmt)
  924. return modeD;
  925. }
  926. if (++modeU < gspca_dev->cam.nmodes) {
  927. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  928. == pixfmt)
  929. return modeU;
  930. }
  931. }
  932. return -EINVAL;
  933. }
  934. #ifdef CONFIG_VIDEO_ADV_DEBUG
  935. static int vidioc_g_register(struct file *file, void *priv,
  936. struct v4l2_dbg_register *reg)
  937. {
  938. struct gspca_dev *gspca_dev = video_drvdata(file);
  939. gspca_dev->usb_err = 0;
  940. return gspca_dev->sd_desc->get_register(gspca_dev, reg);
  941. }
  942. static int vidioc_s_register(struct file *file, void *priv,
  943. struct v4l2_dbg_register *reg)
  944. {
  945. struct gspca_dev *gspca_dev = video_drvdata(file);
  946. gspca_dev->usb_err = 0;
  947. return gspca_dev->sd_desc->set_register(gspca_dev, reg);
  948. }
  949. #endif
  950. static int vidioc_g_chip_ident(struct file *file, void *priv,
  951. struct v4l2_dbg_chip_ident *chip)
  952. {
  953. struct gspca_dev *gspca_dev = video_drvdata(file);
  954. gspca_dev->usb_err = 0;
  955. return gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
  956. }
  957. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  958. struct v4l2_fmtdesc *fmtdesc)
  959. {
  960. struct gspca_dev *gspca_dev = video_drvdata(file);
  961. int i, j, index;
  962. __u32 fmt_tb[8];
  963. /* give an index to each format */
  964. index = 0;
  965. j = 0;
  966. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  967. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  968. j = 0;
  969. for (;;) {
  970. if (fmt_tb[j] == fmt_tb[index])
  971. break;
  972. j++;
  973. }
  974. if (j == index) {
  975. if (fmtdesc->index == index)
  976. break; /* new format */
  977. index++;
  978. if (index >= ARRAY_SIZE(fmt_tb))
  979. return -EINVAL;
  980. }
  981. }
  982. if (i < 0)
  983. return -EINVAL; /* no more format */
  984. fmtdesc->pixelformat = fmt_tb[index];
  985. if (gspca_dev->cam.cam_mode[i].sizeimage <
  986. gspca_dev->cam.cam_mode[i].width *
  987. gspca_dev->cam.cam_mode[i].height)
  988. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  989. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  990. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  991. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  992. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  993. fmtdesc->description[4] = '\0';
  994. return 0;
  995. }
  996. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  997. struct v4l2_format *fmt)
  998. {
  999. struct gspca_dev *gspca_dev = video_drvdata(file);
  1000. int mode;
  1001. mode = gspca_dev->curr_mode;
  1002. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  1003. /* some drivers use priv internally, zero it before giving it to
  1004. userspace */
  1005. fmt->fmt.pix.priv = 0;
  1006. return 0;
  1007. }
  1008. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  1009. struct v4l2_format *fmt)
  1010. {
  1011. int w, h, mode, mode2;
  1012. w = fmt->fmt.pix.width;
  1013. h = fmt->fmt.pix.height;
  1014. #ifdef GSPCA_DEBUG
  1015. if (gspca_debug & D_CONF)
  1016. PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
  1017. #endif
  1018. /* search the closest mode for width and height */
  1019. mode = wxh_to_mode(gspca_dev, w, h);
  1020. /* OK if right palette */
  1021. if (gspca_dev->cam.cam_mode[mode].pixelformat
  1022. != fmt->fmt.pix.pixelformat) {
  1023. /* else, search the closest mode with the same pixel format */
  1024. mode2 = gspca_get_mode(gspca_dev, mode,
  1025. fmt->fmt.pix.pixelformat);
  1026. if (mode2 >= 0)
  1027. mode = mode2;
  1028. /* else
  1029. ; * no chance, return this mode */
  1030. }
  1031. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  1032. /* some drivers use priv internally, zero it before giving it to
  1033. userspace */
  1034. fmt->fmt.pix.priv = 0;
  1035. return mode; /* used when s_fmt */
  1036. }
  1037. static int vidioc_try_fmt_vid_cap(struct file *file,
  1038. void *priv,
  1039. struct v4l2_format *fmt)
  1040. {
  1041. struct gspca_dev *gspca_dev = video_drvdata(file);
  1042. int ret;
  1043. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1044. if (ret < 0)
  1045. return ret;
  1046. return 0;
  1047. }
  1048. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1049. struct v4l2_format *fmt)
  1050. {
  1051. struct gspca_dev *gspca_dev = video_drvdata(file);
  1052. int ret;
  1053. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1054. return -ERESTARTSYS;
  1055. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1056. if (ret < 0)
  1057. goto out;
  1058. if (gspca_dev->nframes != 0
  1059. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  1060. ret = -EINVAL;
  1061. goto out;
  1062. }
  1063. if (ret == gspca_dev->curr_mode) {
  1064. ret = 0;
  1065. goto out; /* same mode */
  1066. }
  1067. if (gspca_dev->streaming) {
  1068. ret = -EBUSY;
  1069. goto out;
  1070. }
  1071. gspca_dev->width = fmt->fmt.pix.width;
  1072. gspca_dev->height = fmt->fmt.pix.height;
  1073. gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
  1074. gspca_dev->curr_mode = ret;
  1075. ret = 0;
  1076. out:
  1077. mutex_unlock(&gspca_dev->queue_lock);
  1078. return ret;
  1079. }
  1080. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1081. struct v4l2_frmsizeenum *fsize)
  1082. {
  1083. struct gspca_dev *gspca_dev = video_drvdata(file);
  1084. int i;
  1085. __u32 index = 0;
  1086. for (i = 0; i < gspca_dev->cam.nmodes; i++) {
  1087. if (fsize->pixel_format !=
  1088. gspca_dev->cam.cam_mode[i].pixelformat)
  1089. continue;
  1090. if (fsize->index == index) {
  1091. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1092. fsize->discrete.width =
  1093. gspca_dev->cam.cam_mode[i].width;
  1094. fsize->discrete.height =
  1095. gspca_dev->cam.cam_mode[i].height;
  1096. return 0;
  1097. }
  1098. index++;
  1099. }
  1100. return -EINVAL;
  1101. }
  1102. static int vidioc_enum_frameintervals(struct file *filp, void *priv,
  1103. struct v4l2_frmivalenum *fival)
  1104. {
  1105. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1106. int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
  1107. __u32 i;
  1108. if (gspca_dev->cam.mode_framerates == NULL ||
  1109. gspca_dev->cam.mode_framerates[mode].nrates == 0)
  1110. return -EINVAL;
  1111. if (fival->pixel_format !=
  1112. gspca_dev->cam.cam_mode[mode].pixelformat)
  1113. return -EINVAL;
  1114. for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
  1115. if (fival->index == i) {
  1116. fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1117. fival->discrete.numerator = 1;
  1118. fival->discrete.denominator =
  1119. gspca_dev->cam.mode_framerates[mode].rates[i];
  1120. return 0;
  1121. }
  1122. }
  1123. return -EINVAL;
  1124. }
  1125. static void gspca_release(struct v4l2_device *v4l2_device)
  1126. {
  1127. struct gspca_dev *gspca_dev =
  1128. container_of(v4l2_device, struct gspca_dev, v4l2_dev);
  1129. PDEBUG(D_PROBE, "%s released",
  1130. video_device_node_name(&gspca_dev->vdev));
  1131. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1132. v4l2_device_unregister(&gspca_dev->v4l2_dev);
  1133. kfree(gspca_dev->usb_buf);
  1134. kfree(gspca_dev);
  1135. }
  1136. static int dev_open(struct file *file)
  1137. {
  1138. struct gspca_dev *gspca_dev = video_drvdata(file);
  1139. PDEBUG(D_STREAM, "[%s] open", current->comm);
  1140. /* protect the subdriver against rmmod */
  1141. if (!try_module_get(gspca_dev->module))
  1142. return -ENODEV;
  1143. #ifdef GSPCA_DEBUG
  1144. /* activate the v4l2 debug */
  1145. if (gspca_debug & D_V4L2)
  1146. gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
  1147. | V4L2_DEBUG_IOCTL_ARG;
  1148. else
  1149. gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
  1150. | V4L2_DEBUG_IOCTL_ARG);
  1151. #endif
  1152. return v4l2_fh_open(file);
  1153. }
  1154. static int dev_close(struct file *file)
  1155. {
  1156. struct gspca_dev *gspca_dev = video_drvdata(file);
  1157. PDEBUG(D_STREAM, "[%s] close", current->comm);
  1158. /* Needed for gspca_stream_off, always lock before queue_lock! */
  1159. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1160. return -ERESTARTSYS;
  1161. if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
  1162. mutex_unlock(&gspca_dev->usb_lock);
  1163. return -ERESTARTSYS;
  1164. }
  1165. /* if the file did the capture, free the streaming resources */
  1166. if (gspca_dev->capt_file == file) {
  1167. if (gspca_dev->streaming)
  1168. gspca_stream_off(gspca_dev);
  1169. frame_free(gspca_dev);
  1170. }
  1171. module_put(gspca_dev->module);
  1172. mutex_unlock(&gspca_dev->queue_lock);
  1173. mutex_unlock(&gspca_dev->usb_lock);
  1174. PDEBUG(D_STREAM, "close done");
  1175. return v4l2_fh_release(file);
  1176. }
  1177. static int vidioc_querycap(struct file *file, void *priv,
  1178. struct v4l2_capability *cap)
  1179. {
  1180. struct gspca_dev *gspca_dev = video_drvdata(file);
  1181. strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
  1182. sizeof cap->driver);
  1183. if (gspca_dev->dev->product != NULL) {
  1184. strlcpy((char *) cap->card, gspca_dev->dev->product,
  1185. sizeof cap->card);
  1186. } else {
  1187. snprintf((char *) cap->card, sizeof cap->card,
  1188. "USB Camera (%04x:%04x)",
  1189. le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
  1190. le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
  1191. }
  1192. usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
  1193. sizeof(cap->bus_info));
  1194. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  1195. | V4L2_CAP_STREAMING
  1196. | V4L2_CAP_READWRITE;
  1197. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1198. return 0;
  1199. }
  1200. static int get_ctrl(struct gspca_dev *gspca_dev,
  1201. int id)
  1202. {
  1203. const struct ctrl *ctrls;
  1204. int i;
  1205. for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
  1206. i < gspca_dev->sd_desc->nctrls;
  1207. i++, ctrls++) {
  1208. if (gspca_dev->ctrl_dis & (1 << i))
  1209. continue;
  1210. if (id == ctrls->qctrl.id)
  1211. return i;
  1212. }
  1213. return -1;
  1214. }
  1215. static int vidioc_queryctrl(struct file *file, void *priv,
  1216. struct v4l2_queryctrl *q_ctrl)
  1217. {
  1218. struct gspca_dev *gspca_dev = video_drvdata(file);
  1219. const struct ctrl *ctrls;
  1220. struct gspca_ctrl *gspca_ctrl;
  1221. int i, idx;
  1222. u32 id;
  1223. id = q_ctrl->id;
  1224. if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  1225. id &= V4L2_CTRL_ID_MASK;
  1226. id++;
  1227. idx = -1;
  1228. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  1229. if (gspca_dev->ctrl_dis & (1 << i))
  1230. continue;
  1231. if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
  1232. continue;
  1233. if (idx >= 0
  1234. && gspca_dev->sd_desc->ctrls[i].qctrl.id
  1235. > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
  1236. continue;
  1237. idx = i;
  1238. }
  1239. } else {
  1240. idx = get_ctrl(gspca_dev, id);
  1241. }
  1242. if (idx < 0)
  1243. return -EINVAL;
  1244. ctrls = &gspca_dev->sd_desc->ctrls[idx];
  1245. memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
  1246. if (gspca_dev->cam.ctrls != NULL) {
  1247. gspca_ctrl = &gspca_dev->cam.ctrls[idx];
  1248. q_ctrl->default_value = gspca_ctrl->def;
  1249. q_ctrl->minimum = gspca_ctrl->min;
  1250. q_ctrl->maximum = gspca_ctrl->max;
  1251. }
  1252. if (gspca_dev->ctrl_inac & (1 << idx))
  1253. q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
  1254. return 0;
  1255. }
  1256. static int vidioc_s_ctrl(struct file *file, void *priv,
  1257. struct v4l2_control *ctrl)
  1258. {
  1259. struct gspca_dev *gspca_dev = video_drvdata(file);
  1260. const struct ctrl *ctrls;
  1261. struct gspca_ctrl *gspca_ctrl;
  1262. int idx;
  1263. idx = get_ctrl(gspca_dev, ctrl->id);
  1264. if (idx < 0)
  1265. return -EINVAL;
  1266. if (gspca_dev->ctrl_inac & (1 << idx))
  1267. return -EINVAL;
  1268. ctrls = &gspca_dev->sd_desc->ctrls[idx];
  1269. if (gspca_dev->cam.ctrls != NULL) {
  1270. gspca_ctrl = &gspca_dev->cam.ctrls[idx];
  1271. if (ctrl->value < gspca_ctrl->min
  1272. || ctrl->value > gspca_ctrl->max)
  1273. return -ERANGE;
  1274. } else {
  1275. gspca_ctrl = NULL;
  1276. if (ctrl->value < ctrls->qctrl.minimum
  1277. || ctrl->value > ctrls->qctrl.maximum)
  1278. return -ERANGE;
  1279. }
  1280. PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
  1281. gspca_dev->usb_err = 0;
  1282. if (ctrls->set != NULL)
  1283. return ctrls->set(gspca_dev, ctrl->value);
  1284. if (gspca_ctrl != NULL) {
  1285. gspca_ctrl->val = ctrl->value;
  1286. if (ctrls->set_control != NULL
  1287. && gspca_dev->streaming)
  1288. ctrls->set_control(gspca_dev);
  1289. }
  1290. return gspca_dev->usb_err;
  1291. }
  1292. static int vidioc_g_ctrl(struct file *file, void *priv,
  1293. struct v4l2_control *ctrl)
  1294. {
  1295. struct gspca_dev *gspca_dev = video_drvdata(file);
  1296. const struct ctrl *ctrls;
  1297. int idx;
  1298. idx = get_ctrl(gspca_dev, ctrl->id);
  1299. if (idx < 0)
  1300. return -EINVAL;
  1301. ctrls = &gspca_dev->sd_desc->ctrls[idx];
  1302. gspca_dev->usb_err = 0;
  1303. if (ctrls->get != NULL)
  1304. return ctrls->get(gspca_dev, &ctrl->value);
  1305. if (gspca_dev->cam.ctrls != NULL)
  1306. ctrl->value = gspca_dev->cam.ctrls[idx].val;
  1307. return 0;
  1308. }
  1309. static int vidioc_querymenu(struct file *file, void *priv,
  1310. struct v4l2_querymenu *qmenu)
  1311. {
  1312. struct gspca_dev *gspca_dev = video_drvdata(file);
  1313. if (!gspca_dev->sd_desc->querymenu)
  1314. return -ENOTTY;
  1315. return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
  1316. }
  1317. static int vidioc_enum_input(struct file *file, void *priv,
  1318. struct v4l2_input *input)
  1319. {
  1320. struct gspca_dev *gspca_dev = video_drvdata(file);
  1321. if (input->index != 0)
  1322. return -EINVAL;
  1323. input->type = V4L2_INPUT_TYPE_CAMERA;
  1324. input->status = gspca_dev->cam.input_flags;
  1325. strlcpy(input->name, gspca_dev->sd_desc->name,
  1326. sizeof input->name);
  1327. return 0;
  1328. }
  1329. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1330. {
  1331. *i = 0;
  1332. return 0;
  1333. }
  1334. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1335. {
  1336. if (i > 0)
  1337. return -EINVAL;
  1338. return (0);
  1339. }
  1340. static int vidioc_reqbufs(struct file *file, void *priv,
  1341. struct v4l2_requestbuffers *rb)
  1342. {
  1343. struct gspca_dev *gspca_dev = video_drvdata(file);
  1344. int i, ret = 0, streaming;
  1345. i = rb->memory; /* (avoid compilation warning) */
  1346. switch (i) {
  1347. case GSPCA_MEMORY_READ: /* (internal call) */
  1348. case V4L2_MEMORY_MMAP:
  1349. case V4L2_MEMORY_USERPTR:
  1350. break;
  1351. default:
  1352. return -EINVAL;
  1353. }
  1354. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1355. return -ERESTARTSYS;
  1356. if (gspca_dev->memory != GSPCA_MEMORY_NO
  1357. && gspca_dev->memory != GSPCA_MEMORY_READ
  1358. && gspca_dev->memory != rb->memory) {
  1359. ret = -EBUSY;
  1360. goto out;
  1361. }
  1362. /* only one file may do the capture */
  1363. if (gspca_dev->capt_file != NULL
  1364. && gspca_dev->capt_file != file) {
  1365. ret = -EBUSY;
  1366. goto out;
  1367. }
  1368. /* if allocated, the buffers must not be mapped */
  1369. for (i = 0; i < gspca_dev->nframes; i++) {
  1370. if (gspca_dev->frame[i].vma_use_count) {
  1371. ret = -EBUSY;
  1372. goto out;
  1373. }
  1374. }
  1375. /* stop streaming */
  1376. streaming = gspca_dev->streaming;
  1377. if (streaming) {
  1378. gspca_stream_off(gspca_dev);
  1379. /* Don't restart the stream when switching from read
  1380. * to mmap mode */
  1381. if (gspca_dev->memory == GSPCA_MEMORY_READ)
  1382. streaming = 0;
  1383. }
  1384. /* free the previous allocated buffers, if any */
  1385. if (gspca_dev->nframes != 0)
  1386. frame_free(gspca_dev);
  1387. if (rb->count == 0) /* unrequest */
  1388. goto out;
  1389. ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
  1390. if (ret == 0) {
  1391. rb->count = gspca_dev->nframes;
  1392. if (streaming)
  1393. ret = gspca_init_transfer(gspca_dev);
  1394. }
  1395. out:
  1396. mutex_unlock(&gspca_dev->queue_lock);
  1397. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  1398. return ret;
  1399. }
  1400. static int vidioc_querybuf(struct file *file, void *priv,
  1401. struct v4l2_buffer *v4l2_buf)
  1402. {
  1403. struct gspca_dev *gspca_dev = video_drvdata(file);
  1404. struct gspca_frame *frame;
  1405. if (v4l2_buf->index < 0
  1406. || v4l2_buf->index >= gspca_dev->nframes)
  1407. return -EINVAL;
  1408. frame = &gspca_dev->frame[v4l2_buf->index];
  1409. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1410. return 0;
  1411. }
  1412. static int vidioc_streamon(struct file *file, void *priv,
  1413. enum v4l2_buf_type buf_type)
  1414. {
  1415. struct gspca_dev *gspca_dev = video_drvdata(file);
  1416. int ret;
  1417. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1418. return -EINVAL;
  1419. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1420. return -ERESTARTSYS;
  1421. /* check the capture file */
  1422. if (gspca_dev->capt_file != file) {
  1423. ret = -EBUSY;
  1424. goto out;
  1425. }
  1426. if (gspca_dev->nframes == 0
  1427. || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
  1428. ret = -EINVAL;
  1429. goto out;
  1430. }
  1431. if (!gspca_dev->streaming) {
  1432. ret = gspca_init_transfer(gspca_dev);
  1433. if (ret < 0)
  1434. goto out;
  1435. }
  1436. #ifdef GSPCA_DEBUG
  1437. if (gspca_debug & D_STREAM) {
  1438. PDEBUG_MODE("stream on OK",
  1439. gspca_dev->pixfmt,
  1440. gspca_dev->width,
  1441. gspca_dev->height);
  1442. }
  1443. #endif
  1444. ret = 0;
  1445. out:
  1446. mutex_unlock(&gspca_dev->queue_lock);
  1447. return ret;
  1448. }
  1449. static int vidioc_streamoff(struct file *file, void *priv,
  1450. enum v4l2_buf_type buf_type)
  1451. {
  1452. struct gspca_dev *gspca_dev = video_drvdata(file);
  1453. int i, ret;
  1454. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1455. return -EINVAL;
  1456. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1457. return -ERESTARTSYS;
  1458. if (!gspca_dev->streaming) {
  1459. ret = 0;
  1460. goto out;
  1461. }
  1462. /* check the capture file */
  1463. if (gspca_dev->capt_file != file) {
  1464. ret = -EBUSY;
  1465. goto out;
  1466. }
  1467. /* stop streaming */
  1468. gspca_stream_off(gspca_dev);
  1469. /* In case another thread is waiting in dqbuf */
  1470. wake_up_interruptible(&gspca_dev->wq);
  1471. /* empty the transfer queues */
  1472. for (i = 0; i < gspca_dev->nframes; i++)
  1473. gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
  1474. atomic_set(&gspca_dev->fr_q, 0);
  1475. atomic_set(&gspca_dev->fr_i, 0);
  1476. gspca_dev->fr_o = 0;
  1477. ret = 0;
  1478. out:
  1479. mutex_unlock(&gspca_dev->queue_lock);
  1480. return ret;
  1481. }
  1482. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1483. struct v4l2_jpegcompression *jpegcomp)
  1484. {
  1485. struct gspca_dev *gspca_dev = video_drvdata(file);
  1486. gspca_dev->usb_err = 0;
  1487. return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1488. }
  1489. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1490. struct v4l2_jpegcompression *jpegcomp)
  1491. {
  1492. struct gspca_dev *gspca_dev = video_drvdata(file);
  1493. gspca_dev->usb_err = 0;
  1494. return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1495. }
  1496. static int vidioc_g_parm(struct file *filp, void *priv,
  1497. struct v4l2_streamparm *parm)
  1498. {
  1499. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1500. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1501. if (gspca_dev->sd_desc->get_streamparm) {
  1502. gspca_dev->usb_err = 0;
  1503. gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
  1504. return gspca_dev->usb_err;
  1505. }
  1506. return 0;
  1507. }
  1508. static int vidioc_s_parm(struct file *filp, void *priv,
  1509. struct v4l2_streamparm *parm)
  1510. {
  1511. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1512. int n;
  1513. n = parm->parm.capture.readbuffers;
  1514. if (n == 0 || n >= GSPCA_MAX_FRAMES)
  1515. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1516. else
  1517. gspca_dev->nbufread = n;
  1518. if (gspca_dev->sd_desc->set_streamparm) {
  1519. gspca_dev->usb_err = 0;
  1520. gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
  1521. return gspca_dev->usb_err;
  1522. }
  1523. return 0;
  1524. }
  1525. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1526. {
  1527. struct gspca_dev *gspca_dev = video_drvdata(file);
  1528. struct gspca_frame *frame;
  1529. struct page *page;
  1530. unsigned long addr, start, size;
  1531. int i, ret;
  1532. start = vma->vm_start;
  1533. size = vma->vm_end - vma->vm_start;
  1534. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1535. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1536. return -ERESTARTSYS;
  1537. if (gspca_dev->capt_file != file) {
  1538. ret = -EINVAL;
  1539. goto out;
  1540. }
  1541. frame = NULL;
  1542. for (i = 0; i < gspca_dev->nframes; ++i) {
  1543. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1544. PDEBUG(D_STREAM, "mmap bad memory type");
  1545. break;
  1546. }
  1547. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1548. == vma->vm_pgoff) {
  1549. frame = &gspca_dev->frame[i];
  1550. break;
  1551. }
  1552. }
  1553. if (frame == NULL) {
  1554. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1555. ret = -EINVAL;
  1556. goto out;
  1557. }
  1558. if (size != frame->v4l2_buf.length) {
  1559. PDEBUG(D_STREAM, "mmap bad size");
  1560. ret = -EINVAL;
  1561. goto out;
  1562. }
  1563. /*
  1564. * - VM_IO marks the area as being a mmaped region for I/O to a
  1565. * device. It also prevents the region from being core dumped.
  1566. */
  1567. vma->vm_flags |= VM_IO;
  1568. addr = (unsigned long) frame->data;
  1569. while (size > 0) {
  1570. page = vmalloc_to_page((void *) addr);
  1571. ret = vm_insert_page(vma, start, page);
  1572. if (ret < 0)
  1573. goto out;
  1574. start += PAGE_SIZE;
  1575. addr += PAGE_SIZE;
  1576. size -= PAGE_SIZE;
  1577. }
  1578. vma->vm_ops = &gspca_vm_ops;
  1579. vma->vm_private_data = frame;
  1580. gspca_vm_open(vma);
  1581. ret = 0;
  1582. out:
  1583. mutex_unlock(&gspca_dev->queue_lock);
  1584. return ret;
  1585. }
  1586. static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
  1587. enum v4l2_memory memory)
  1588. {
  1589. if (!gspca_dev->present)
  1590. return -ENODEV;
  1591. if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
  1592. !gspca_dev->streaming)
  1593. return -EINVAL;
  1594. /* check if a frame is ready */
  1595. return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
  1596. }
  1597. static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
  1598. enum v4l2_memory memory)
  1599. {
  1600. int ret;
  1601. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1602. return -ERESTARTSYS;
  1603. ret = frame_ready_nolock(gspca_dev, file, memory);
  1604. mutex_unlock(&gspca_dev->queue_lock);
  1605. return ret;
  1606. }
  1607. /*
  1608. * dequeue a video buffer
  1609. *
  1610. * If nonblock_ing is false, block until a buffer is available.
  1611. */
  1612. static int vidioc_dqbuf(struct file *file, void *priv,
  1613. struct v4l2_buffer *v4l2_buf)
  1614. {
  1615. struct gspca_dev *gspca_dev = video_drvdata(file);
  1616. struct gspca_frame *frame;
  1617. int i, j, ret;
  1618. PDEBUG(D_FRAM, "dqbuf");
  1619. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1620. return -ERESTARTSYS;
  1621. for (;;) {
  1622. ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
  1623. if (ret < 0)
  1624. goto out;
  1625. if (ret > 0)
  1626. break;
  1627. mutex_unlock(&gspca_dev->queue_lock);
  1628. if (file->f_flags & O_NONBLOCK)
  1629. return -EAGAIN;
  1630. /* wait till a frame is ready */
  1631. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1632. frame_ready(gspca_dev, file, v4l2_buf->memory),
  1633. msecs_to_jiffies(3000));
  1634. if (ret < 0)
  1635. return ret;
  1636. if (ret == 0)
  1637. return -EIO;
  1638. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1639. return -ERESTARTSYS;
  1640. }
  1641. i = gspca_dev->fr_o;
  1642. j = gspca_dev->fr_queue[i];
  1643. frame = &gspca_dev->frame[j];
  1644. gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
  1645. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1646. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1647. PDEBUG(D_FRAM, "dqbuf %d", j);
  1648. ret = 0;
  1649. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1650. if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
  1651. frame->data,
  1652. frame->v4l2_buf.bytesused)) {
  1653. PDEBUG(D_ERR|D_STREAM,
  1654. "dqbuf cp to user failed");
  1655. ret = -EFAULT;
  1656. }
  1657. }
  1658. out:
  1659. mutex_unlock(&gspca_dev->queue_lock);
  1660. if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
  1661. mutex_lock(&gspca_dev->usb_lock);
  1662. gspca_dev->usb_err = 0;
  1663. if (gspca_dev->present)
  1664. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1665. mutex_unlock(&gspca_dev->usb_lock);
  1666. }
  1667. return ret;
  1668. }
  1669. /*
  1670. * queue a video buffer
  1671. *
  1672. * Attempting to queue a buffer that has already been
  1673. * queued will return -EINVAL.
  1674. */
  1675. static int vidioc_qbuf(struct file *file, void *priv,
  1676. struct v4l2_buffer *v4l2_buf)
  1677. {
  1678. struct gspca_dev *gspca_dev = video_drvdata(file);
  1679. struct gspca_frame *frame;
  1680. int i, index, ret;
  1681. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1682. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1683. return -ERESTARTSYS;
  1684. index = v4l2_buf->index;
  1685. if ((unsigned) index >= gspca_dev->nframes) {
  1686. PDEBUG(D_FRAM,
  1687. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1688. ret = -EINVAL;
  1689. goto out;
  1690. }
  1691. if (v4l2_buf->memory != gspca_dev->memory) {
  1692. PDEBUG(D_FRAM, "qbuf bad memory type");
  1693. ret = -EINVAL;
  1694. goto out;
  1695. }
  1696. frame = &gspca_dev->frame[index];
  1697. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1698. PDEBUG(D_FRAM, "qbuf bad state");
  1699. ret = -EINVAL;
  1700. goto out;
  1701. }
  1702. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1703. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1704. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1705. frame->v4l2_buf.length = v4l2_buf->length;
  1706. }
  1707. /* put the buffer in the 'queued' queue */
  1708. i = atomic_read(&gspca_dev->fr_q);
  1709. gspca_dev->fr_queue[i] = index;
  1710. atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
  1711. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1712. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1713. ret = 0;
  1714. out:
  1715. mutex_unlock(&gspca_dev->queue_lock);
  1716. return ret;
  1717. }
  1718. /*
  1719. * allocate the resources for read()
  1720. */
  1721. static int read_alloc(struct gspca_dev *gspca_dev,
  1722. struct file *file)
  1723. {
  1724. struct v4l2_buffer v4l2_buf;
  1725. int i, ret;
  1726. PDEBUG(D_STREAM, "read alloc");
  1727. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1728. return -ERESTARTSYS;
  1729. if (gspca_dev->nframes == 0) {
  1730. struct v4l2_requestbuffers rb;
  1731. memset(&rb, 0, sizeof rb);
  1732. rb.count = gspca_dev->nbufread;
  1733. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1734. rb.memory = GSPCA_MEMORY_READ;
  1735. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1736. if (ret != 0) {
  1737. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1738. goto out;
  1739. }
  1740. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1741. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1742. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1743. for (i = 0; i < gspca_dev->nbufread; i++) {
  1744. v4l2_buf.index = i;
  1745. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1746. if (ret != 0) {
  1747. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1748. goto out;
  1749. }
  1750. }
  1751. }
  1752. /* start streaming */
  1753. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1754. if (ret != 0)
  1755. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1756. out:
  1757. mutex_unlock(&gspca_dev->usb_lock);
  1758. return ret;
  1759. }
  1760. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1761. {
  1762. struct gspca_dev *gspca_dev = video_drvdata(file);
  1763. unsigned long req_events = poll_requested_events(wait);
  1764. int ret = 0;
  1765. PDEBUG(D_FRAM, "poll");
  1766. if (req_events & POLLPRI)
  1767. ret |= v4l2_ctrl_poll(file, wait);
  1768. if (req_events & (POLLIN | POLLRDNORM)) {
  1769. /* if reqbufs is not done, the user would use read() */
  1770. if (gspca_dev->memory == GSPCA_MEMORY_NO) {
  1771. if (read_alloc(gspca_dev, file) != 0) {
  1772. ret |= POLLERR;
  1773. goto out;
  1774. }
  1775. }
  1776. poll_wait(file, &gspca_dev->wq, wait);
  1777. /* check if an image has been received */
  1778. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
  1779. ret |= POLLERR;
  1780. goto out;
  1781. }
  1782. if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
  1783. ret |= POLLIN | POLLRDNORM;
  1784. mutex_unlock(&gspca_dev->queue_lock);
  1785. }
  1786. out:
  1787. if (!gspca_dev->present)
  1788. ret |= POLLHUP;
  1789. return ret;
  1790. }
  1791. static ssize_t dev_read(struct file *file, char __user *data,
  1792. size_t count, loff_t *ppos)
  1793. {
  1794. struct gspca_dev *gspca_dev = video_drvdata(file);
  1795. struct gspca_frame *frame;
  1796. struct v4l2_buffer v4l2_buf;
  1797. struct timeval timestamp;
  1798. int n, ret, ret2;
  1799. PDEBUG(D_FRAM, "read (%zd)", count);
  1800. if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
  1801. ret = read_alloc(gspca_dev, file);
  1802. if (ret != 0)
  1803. return ret;
  1804. }
  1805. /* get a frame */
  1806. timestamp = ktime_to_timeval(ktime_get());
  1807. timestamp.tv_sec--;
  1808. n = 2;
  1809. for (;;) {
  1810. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1811. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1812. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1813. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1814. if (ret != 0) {
  1815. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1816. return ret;
  1817. }
  1818. /* if the process slept for more than 1 second,
  1819. * get a newer frame */
  1820. frame = &gspca_dev->frame[v4l2_buf.index];
  1821. if (--n < 0)
  1822. break; /* avoid infinite loop */
  1823. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1824. break;
  1825. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1826. if (ret != 0) {
  1827. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1828. return ret;
  1829. }
  1830. }
  1831. /* copy the frame */
  1832. if (count > frame->v4l2_buf.bytesused)
  1833. count = frame->v4l2_buf.bytesused;
  1834. ret = copy_to_user(data, frame->data, count);
  1835. if (ret != 0) {
  1836. PDEBUG(D_ERR|D_STREAM,
  1837. "read cp to user lack %d / %zd", ret, count);
  1838. ret = -EFAULT;
  1839. goto out;
  1840. }
  1841. ret = count;
  1842. out:
  1843. /* in each case, requeue the buffer */
  1844. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1845. if (ret2 != 0)
  1846. return ret2;
  1847. return ret;
  1848. }
  1849. static struct v4l2_file_operations dev_fops = {
  1850. .owner = THIS_MODULE,
  1851. .open = dev_open,
  1852. .release = dev_close,
  1853. .read = dev_read,
  1854. .mmap = dev_mmap,
  1855. .unlocked_ioctl = video_ioctl2,
  1856. .poll = dev_poll,
  1857. };
  1858. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1859. .vidioc_querycap = vidioc_querycap,
  1860. .vidioc_dqbuf = vidioc_dqbuf,
  1861. .vidioc_qbuf = vidioc_qbuf,
  1862. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1863. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1864. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1865. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1866. .vidioc_streamon = vidioc_streamon,
  1867. .vidioc_queryctrl = vidioc_queryctrl,
  1868. .vidioc_g_ctrl = vidioc_g_ctrl,
  1869. .vidioc_s_ctrl = vidioc_s_ctrl,
  1870. .vidioc_querymenu = vidioc_querymenu,
  1871. .vidioc_enum_input = vidioc_enum_input,
  1872. .vidioc_g_input = vidioc_g_input,
  1873. .vidioc_s_input = vidioc_s_input,
  1874. .vidioc_reqbufs = vidioc_reqbufs,
  1875. .vidioc_querybuf = vidioc_querybuf,
  1876. .vidioc_streamoff = vidioc_streamoff,
  1877. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1878. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1879. .vidioc_g_parm = vidioc_g_parm,
  1880. .vidioc_s_parm = vidioc_s_parm,
  1881. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1882. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  1883. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1884. .vidioc_g_register = vidioc_g_register,
  1885. .vidioc_s_register = vidioc_s_register,
  1886. #endif
  1887. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1888. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1889. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1890. };
  1891. static const struct video_device gspca_template = {
  1892. .name = "gspca main driver",
  1893. .fops = &dev_fops,
  1894. .ioctl_ops = &dev_ioctl_ops,
  1895. .release = video_device_release_empty, /* We use v4l2_dev.release */
  1896. };
  1897. /* initialize the controls */
  1898. static void ctrls_init(struct gspca_dev *gspca_dev)
  1899. {
  1900. struct gspca_ctrl *ctrl;
  1901. int i;
  1902. for (i = 0, ctrl = gspca_dev->cam.ctrls;
  1903. i < gspca_dev->sd_desc->nctrls;
  1904. i++, ctrl++) {
  1905. ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
  1906. ctrl->val = ctrl->def;
  1907. ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
  1908. ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
  1909. }
  1910. }
  1911. /*
  1912. * probe and create a new gspca device
  1913. *
  1914. * This function must be called by the sub-driver when it is
  1915. * called for probing a new device.
  1916. */
  1917. int gspca_dev_probe2(struct usb_interface *intf,
  1918. const struct usb_device_id *id,
  1919. const struct sd_desc *sd_desc,
  1920. int dev_size,
  1921. struct module *module)
  1922. {
  1923. struct gspca_dev *gspca_dev;
  1924. struct usb_device *dev = interface_to_usbdev(intf);
  1925. int ret;
  1926. pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
  1927. sd_desc->name, id->idVendor, id->idProduct);
  1928. /* create the device */
  1929. if (dev_size < sizeof *gspca_dev)
  1930. dev_size = sizeof *gspca_dev;
  1931. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1932. if (!gspca_dev) {
  1933. pr_err("couldn't kzalloc gspca struct\n");
  1934. return -ENOMEM;
  1935. }
  1936. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1937. if (!gspca_dev->usb_buf) {
  1938. pr_err("out of memory\n");
  1939. ret = -ENOMEM;
  1940. goto out;
  1941. }
  1942. gspca_dev->dev = dev;
  1943. gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
  1944. /* check if any audio device */
  1945. if (dev->actconfig->desc.bNumInterfaces != 1) {
  1946. int i;
  1947. struct usb_interface *intf2;
  1948. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1949. intf2 = dev->actconfig->interface[i];
  1950. if (intf2 != NULL
  1951. && intf2->altsetting != NULL
  1952. && intf2->altsetting->desc.bInterfaceClass ==
  1953. USB_CLASS_AUDIO) {
  1954. gspca_dev->audio = 1;
  1955. break;
  1956. }
  1957. }
  1958. }
  1959. gspca_dev->v4l2_dev.release = gspca_release;
  1960. ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
  1961. if (ret)
  1962. goto out;
  1963. gspca_dev->sd_desc = sd_desc;
  1964. gspca_dev->nbufread = 2;
  1965. gspca_dev->empty_packet = -1; /* don't check the empty packets */
  1966. gspca_dev->vdev = gspca_template;
  1967. gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
  1968. video_set_drvdata(&gspca_dev->vdev, gspca_dev);
  1969. set_bit(V4L2_FL_USE_FH_PRIO, &gspca_dev->vdev.flags);
  1970. gspca_dev->module = module;
  1971. gspca_dev->present = 1;
  1972. mutex_init(&gspca_dev->usb_lock);
  1973. gspca_dev->vdev.lock = &gspca_dev->usb_lock;
  1974. mutex_init(&gspca_dev->queue_lock);
  1975. init_waitqueue_head(&gspca_dev->wq);
  1976. /* configure the subdriver and initialize the USB device */
  1977. ret = sd_desc->config(gspca_dev, id);
  1978. if (ret < 0)
  1979. goto out;
  1980. if (gspca_dev->cam.ctrls != NULL)
  1981. ctrls_init(gspca_dev);
  1982. ret = sd_desc->init(gspca_dev);
  1983. if (ret < 0)
  1984. goto out;
  1985. if (sd_desc->init_controls)
  1986. ret = sd_desc->init_controls(gspca_dev);
  1987. if (ret < 0)
  1988. goto out;
  1989. gspca_set_default_mode(gspca_dev);
  1990. ret = gspca_input_connect(gspca_dev);
  1991. if (ret)
  1992. goto out;
  1993. /*
  1994. * Don't take usb_lock for these ioctls. This improves latency if
  1995. * usb_lock is taken for a long time, e.g. when changing a control
  1996. * value, and a new frame is ready to be dequeued.
  1997. */
  1998. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
  1999. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
  2000. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
  2001. if (!gspca_dev->sd_desc->get_chip_ident)
  2002. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_CHIP_IDENT);
  2003. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2004. if (!gspca_dev->sd_desc->get_chip_ident ||
  2005. !gspca_dev->sd_desc->get_register)
  2006. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
  2007. if (!gspca_dev->sd_desc->get_chip_ident ||
  2008. !gspca_dev->sd_desc->set_register)
  2009. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
  2010. #endif
  2011. if (!gspca_dev->sd_desc->get_jcomp)
  2012. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
  2013. if (!gspca_dev->sd_desc->set_jcomp)
  2014. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
  2015. /* init video stuff */
  2016. ret = video_register_device(&gspca_dev->vdev,
  2017. VFL_TYPE_GRABBER,
  2018. -1);
  2019. if (ret < 0) {
  2020. pr_err("video_register_device err %d\n", ret);
  2021. goto out;
  2022. }
  2023. usb_set_intfdata(intf, gspca_dev);
  2024. PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
  2025. gspca_input_create_urb(gspca_dev);
  2026. return 0;
  2027. out:
  2028. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2029. if (gspca_dev->input_dev)
  2030. input_unregister_device(gspca_dev->input_dev);
  2031. #endif
  2032. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  2033. kfree(gspca_dev->usb_buf);
  2034. kfree(gspca_dev);
  2035. return ret;
  2036. }
  2037. EXPORT_SYMBOL(gspca_dev_probe2);
  2038. /* same function as the previous one, but check the interface */
  2039. int gspca_dev_probe(struct usb_interface *intf,
  2040. const struct usb_device_id *id,
  2041. const struct sd_desc *sd_desc,
  2042. int dev_size,
  2043. struct module *module)
  2044. {
  2045. struct usb_device *dev = interface_to_usbdev(intf);
  2046. /* we don't handle multi-config cameras */
  2047. if (dev->descriptor.bNumConfigurations != 1) {
  2048. pr_err("%04x:%04x too many config\n",
  2049. id->idVendor, id->idProduct);
  2050. return -ENODEV;
  2051. }
  2052. /* the USB video interface must be the first one */
  2053. if (dev->actconfig->desc.bNumInterfaces != 1
  2054. && intf->cur_altsetting->desc.bInterfaceNumber != 0)
  2055. return -ENODEV;
  2056. return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
  2057. }
  2058. EXPORT_SYMBOL(gspca_dev_probe);
  2059. /*
  2060. * USB disconnection
  2061. *
  2062. * This function must be called by the sub-driver
  2063. * when the device disconnects, after the specific resources are freed.
  2064. */
  2065. void gspca_disconnect(struct usb_interface *intf)
  2066. {
  2067. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2068. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2069. struct input_dev *input_dev;
  2070. #endif
  2071. PDEBUG(D_PROBE, "%s disconnect",
  2072. video_device_node_name(&gspca_dev->vdev));
  2073. mutex_lock(&gspca_dev->usb_lock);
  2074. usb_set_intfdata(intf, NULL);
  2075. gspca_dev->dev = NULL;
  2076. gspca_dev->present = 0;
  2077. destroy_urbs(gspca_dev);
  2078. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2079. gspca_input_destroy_urb(gspca_dev);
  2080. input_dev = gspca_dev->input_dev;
  2081. if (input_dev) {
  2082. gspca_dev->input_dev = NULL;
  2083. input_unregister_device(input_dev);
  2084. }
  2085. #endif
  2086. /* Free subdriver's streaming resources / stop sd workqueue(s) */
  2087. if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
  2088. gspca_dev->sd_desc->stop0(gspca_dev);
  2089. gspca_dev->streaming = 0;
  2090. wake_up_interruptible(&gspca_dev->wq);
  2091. v4l2_device_disconnect(&gspca_dev->v4l2_dev);
  2092. video_unregister_device(&gspca_dev->vdev);
  2093. mutex_unlock(&gspca_dev->usb_lock);
  2094. /* (this will call gspca_release() immediately or on last close) */
  2095. v4l2_device_put(&gspca_dev->v4l2_dev);
  2096. }
  2097. EXPORT_SYMBOL(gspca_disconnect);
  2098. #ifdef CONFIG_PM
  2099. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  2100. {
  2101. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2102. if (!gspca_dev->streaming)
  2103. return 0;
  2104. mutex_lock(&gspca_dev->usb_lock);
  2105. gspca_dev->frozen = 1; /* avoid urb error messages */
  2106. gspca_dev->usb_err = 0;
  2107. if (gspca_dev->sd_desc->stopN)
  2108. gspca_dev->sd_desc->stopN(gspca_dev);
  2109. destroy_urbs(gspca_dev);
  2110. gspca_input_destroy_urb(gspca_dev);
  2111. gspca_set_alt0(gspca_dev);
  2112. if (gspca_dev->sd_desc->stop0)
  2113. gspca_dev->sd_desc->stop0(gspca_dev);
  2114. mutex_unlock(&gspca_dev->usb_lock);
  2115. return 0;
  2116. }
  2117. EXPORT_SYMBOL(gspca_suspend);
  2118. int gspca_resume(struct usb_interface *intf)
  2119. {
  2120. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2121. int streaming, ret = 0;
  2122. mutex_lock(&gspca_dev->usb_lock);
  2123. gspca_dev->frozen = 0;
  2124. gspca_dev->usb_err = 0;
  2125. gspca_dev->sd_desc->init(gspca_dev);
  2126. gspca_input_create_urb(gspca_dev);
  2127. /*
  2128. * Most subdrivers send all ctrl values on sd_start and thus
  2129. * only write to the device registers on s_ctrl when streaming ->
  2130. * Clear streaming to avoid setting all ctrls twice.
  2131. */
  2132. streaming = gspca_dev->streaming;
  2133. gspca_dev->streaming = 0;
  2134. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  2135. if (streaming)
  2136. ret = gspca_init_transfer(gspca_dev);
  2137. mutex_unlock(&gspca_dev->usb_lock);
  2138. return ret;
  2139. }
  2140. EXPORT_SYMBOL(gspca_resume);
  2141. #endif
  2142. /* -- module insert / remove -- */
  2143. static int __init gspca_init(void)
  2144. {
  2145. pr_info("v" GSPCA_VERSION " registered\n");
  2146. return 0;
  2147. }
  2148. static void __exit gspca_exit(void)
  2149. {
  2150. }
  2151. module_init(gspca_init);
  2152. module_exit(gspca_exit);
  2153. #ifdef GSPCA_DEBUG
  2154. module_param_named(debug, gspca_debug, int, 0644);
  2155. MODULE_PARM_DESC(debug,
  2156. "Debug (bit) 0x01:error 0x02:probe 0x04:config"
  2157. " 0x08:stream 0x10:frame 0x20:packet"
  2158. " 0x0100: v4l2");
  2159. #endif