gspca.c 60 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 IS_ENABLED(CONFIG_INPUT)
  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 IS_ENABLED(CONFIG_INPUT)
  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. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  834. /* some bulk transfers are started by the subdriver */
  835. if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
  836. break;
  837. /* submit the URBs */
  838. for (n = 0; n < MAX_NURBS; n++) {
  839. urb = gspca_dev->urb[n];
  840. if (urb == NULL)
  841. break;
  842. ret = usb_submit_urb(urb, GFP_KERNEL);
  843. if (ret < 0)
  844. break;
  845. }
  846. if (ret >= 0)
  847. break; /* transfer is started */
  848. /* something when wrong
  849. * stop the webcam and free the transfer resources */
  850. gspca_stream_off(gspca_dev);
  851. if (ret != -ENOSPC) {
  852. pr_err("usb_submit_urb alt %d err %d\n",
  853. gspca_dev->alt, ret);
  854. goto out;
  855. }
  856. /* the bandwidth is not wide enough
  857. * negotiate or try a lower alternate setting */
  858. retry:
  859. PDEBUG(D_ERR|D_STREAM,
  860. "alt %d - bandwidth not wide enough - trying again",
  861. alt);
  862. msleep(20); /* wait for kill complete */
  863. if (gspca_dev->sd_desc->isoc_nego) {
  864. ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
  865. if (ret < 0)
  866. goto out;
  867. } else {
  868. if (alt_idx <= 0) {
  869. pr_err("no transfer endpoint found\n");
  870. ret = -EIO;
  871. goto out;
  872. }
  873. gspca_dev->alt = ep_tb[--alt_idx].alt;
  874. }
  875. }
  876. out:
  877. gspca_input_create_urb(gspca_dev);
  878. return ret;
  879. }
  880. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  881. {
  882. struct gspca_ctrl *ctrl;
  883. int i;
  884. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  885. gspca_dev->curr_mode = i;
  886. gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
  887. gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
  888. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
  889. /* set the current control values to their default values
  890. * which may have changed in sd_init() */
  891. /* does nothing if ctrl_handler == NULL */
  892. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  893. ctrl = gspca_dev->cam.ctrls;
  894. if (ctrl != NULL) {
  895. for (i = 0;
  896. i < gspca_dev->sd_desc->nctrls;
  897. i++, ctrl++)
  898. ctrl->val = ctrl->def;
  899. }
  900. }
  901. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  902. int width, int height)
  903. {
  904. int i;
  905. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  906. if (width >= gspca_dev->cam.cam_mode[i].width
  907. && height >= gspca_dev->cam.cam_mode[i].height)
  908. break;
  909. }
  910. return i;
  911. }
  912. /*
  913. * search a mode with the right pixel format
  914. */
  915. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  916. int mode,
  917. int pixfmt)
  918. {
  919. int modeU, modeD;
  920. modeU = modeD = mode;
  921. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  922. if (--modeD >= 0) {
  923. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  924. == pixfmt)
  925. return modeD;
  926. }
  927. if (++modeU < gspca_dev->cam.nmodes) {
  928. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  929. == pixfmt)
  930. return modeU;
  931. }
  932. }
  933. return -EINVAL;
  934. }
  935. #ifdef CONFIG_VIDEO_ADV_DEBUG
  936. static int vidioc_g_register(struct file *file, void *priv,
  937. struct v4l2_dbg_register *reg)
  938. {
  939. struct gspca_dev *gspca_dev = video_drvdata(file);
  940. gspca_dev->usb_err = 0;
  941. return gspca_dev->sd_desc->get_register(gspca_dev, reg);
  942. }
  943. static int vidioc_s_register(struct file *file, void *priv,
  944. struct v4l2_dbg_register *reg)
  945. {
  946. struct gspca_dev *gspca_dev = video_drvdata(file);
  947. gspca_dev->usb_err = 0;
  948. return gspca_dev->sd_desc->set_register(gspca_dev, reg);
  949. }
  950. #endif
  951. static int vidioc_g_chip_ident(struct file *file, void *priv,
  952. struct v4l2_dbg_chip_ident *chip)
  953. {
  954. struct gspca_dev *gspca_dev = video_drvdata(file);
  955. gspca_dev->usb_err = 0;
  956. return gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
  957. }
  958. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  959. struct v4l2_fmtdesc *fmtdesc)
  960. {
  961. struct gspca_dev *gspca_dev = video_drvdata(file);
  962. int i, j, index;
  963. __u32 fmt_tb[8];
  964. /* give an index to each format */
  965. index = 0;
  966. j = 0;
  967. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  968. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  969. j = 0;
  970. for (;;) {
  971. if (fmt_tb[j] == fmt_tb[index])
  972. break;
  973. j++;
  974. }
  975. if (j == index) {
  976. if (fmtdesc->index == index)
  977. break; /* new format */
  978. index++;
  979. if (index >= ARRAY_SIZE(fmt_tb))
  980. return -EINVAL;
  981. }
  982. }
  983. if (i < 0)
  984. return -EINVAL; /* no more format */
  985. fmtdesc->pixelformat = fmt_tb[index];
  986. if (gspca_dev->cam.cam_mode[i].sizeimage <
  987. gspca_dev->cam.cam_mode[i].width *
  988. gspca_dev->cam.cam_mode[i].height)
  989. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  990. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  991. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  992. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  993. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  994. fmtdesc->description[4] = '\0';
  995. return 0;
  996. }
  997. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  998. struct v4l2_format *fmt)
  999. {
  1000. struct gspca_dev *gspca_dev = video_drvdata(file);
  1001. int mode;
  1002. mode = gspca_dev->curr_mode;
  1003. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  1004. /* some drivers use priv internally, zero it before giving it to
  1005. userspace */
  1006. fmt->fmt.pix.priv = 0;
  1007. return 0;
  1008. }
  1009. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  1010. struct v4l2_format *fmt)
  1011. {
  1012. int w, h, mode, mode2;
  1013. w = fmt->fmt.pix.width;
  1014. h = fmt->fmt.pix.height;
  1015. #ifdef GSPCA_DEBUG
  1016. if (gspca_debug & D_CONF)
  1017. PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
  1018. #endif
  1019. /* search the closest mode for width and height */
  1020. mode = wxh_to_mode(gspca_dev, w, h);
  1021. /* OK if right palette */
  1022. if (gspca_dev->cam.cam_mode[mode].pixelformat
  1023. != fmt->fmt.pix.pixelformat) {
  1024. /* else, search the closest mode with the same pixel format */
  1025. mode2 = gspca_get_mode(gspca_dev, mode,
  1026. fmt->fmt.pix.pixelformat);
  1027. if (mode2 >= 0)
  1028. mode = mode2;
  1029. /* else
  1030. ; * no chance, return this mode */
  1031. }
  1032. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  1033. /* some drivers use priv internally, zero it before giving it to
  1034. userspace */
  1035. fmt->fmt.pix.priv = 0;
  1036. return mode; /* used when s_fmt */
  1037. }
  1038. static int vidioc_try_fmt_vid_cap(struct file *file,
  1039. void *priv,
  1040. struct v4l2_format *fmt)
  1041. {
  1042. struct gspca_dev *gspca_dev = video_drvdata(file);
  1043. int ret;
  1044. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1045. if (ret < 0)
  1046. return ret;
  1047. return 0;
  1048. }
  1049. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1050. struct v4l2_format *fmt)
  1051. {
  1052. struct gspca_dev *gspca_dev = video_drvdata(file);
  1053. int ret;
  1054. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1055. return -ERESTARTSYS;
  1056. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1057. if (ret < 0)
  1058. goto out;
  1059. if (gspca_dev->nframes != 0
  1060. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  1061. ret = -EINVAL;
  1062. goto out;
  1063. }
  1064. if (ret == gspca_dev->curr_mode) {
  1065. ret = 0;
  1066. goto out; /* same mode */
  1067. }
  1068. if (gspca_dev->streaming) {
  1069. ret = -EBUSY;
  1070. goto out;
  1071. }
  1072. gspca_dev->width = fmt->fmt.pix.width;
  1073. gspca_dev->height = fmt->fmt.pix.height;
  1074. gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
  1075. gspca_dev->curr_mode = ret;
  1076. ret = 0;
  1077. out:
  1078. mutex_unlock(&gspca_dev->queue_lock);
  1079. return ret;
  1080. }
  1081. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1082. struct v4l2_frmsizeenum *fsize)
  1083. {
  1084. struct gspca_dev *gspca_dev = video_drvdata(file);
  1085. int i;
  1086. __u32 index = 0;
  1087. for (i = 0; i < gspca_dev->cam.nmodes; i++) {
  1088. if (fsize->pixel_format !=
  1089. gspca_dev->cam.cam_mode[i].pixelformat)
  1090. continue;
  1091. if (fsize->index == index) {
  1092. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1093. fsize->discrete.width =
  1094. gspca_dev->cam.cam_mode[i].width;
  1095. fsize->discrete.height =
  1096. gspca_dev->cam.cam_mode[i].height;
  1097. return 0;
  1098. }
  1099. index++;
  1100. }
  1101. return -EINVAL;
  1102. }
  1103. static int vidioc_enum_frameintervals(struct file *filp, void *priv,
  1104. struct v4l2_frmivalenum *fival)
  1105. {
  1106. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1107. int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
  1108. __u32 i;
  1109. if (gspca_dev->cam.mode_framerates == NULL ||
  1110. gspca_dev->cam.mode_framerates[mode].nrates == 0)
  1111. return -EINVAL;
  1112. if (fival->pixel_format !=
  1113. gspca_dev->cam.cam_mode[mode].pixelformat)
  1114. return -EINVAL;
  1115. for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
  1116. if (fival->index == i) {
  1117. fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1118. fival->discrete.numerator = 1;
  1119. fival->discrete.denominator =
  1120. gspca_dev->cam.mode_framerates[mode].rates[i];
  1121. return 0;
  1122. }
  1123. }
  1124. return -EINVAL;
  1125. }
  1126. static void gspca_release(struct v4l2_device *v4l2_device)
  1127. {
  1128. struct gspca_dev *gspca_dev =
  1129. container_of(v4l2_device, struct gspca_dev, v4l2_dev);
  1130. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1131. v4l2_device_unregister(&gspca_dev->v4l2_dev);
  1132. kfree(gspca_dev->usb_buf);
  1133. kfree(gspca_dev);
  1134. }
  1135. static int dev_open(struct file *file)
  1136. {
  1137. struct gspca_dev *gspca_dev = video_drvdata(file);
  1138. PDEBUG(D_STREAM, "[%s] open", current->comm);
  1139. /* protect the subdriver against rmmod */
  1140. if (!try_module_get(gspca_dev->module))
  1141. return -ENODEV;
  1142. #ifdef GSPCA_DEBUG
  1143. /* activate the v4l2 debug */
  1144. if (gspca_debug & D_V4L2)
  1145. gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
  1146. | V4L2_DEBUG_IOCTL_ARG;
  1147. else
  1148. gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
  1149. | V4L2_DEBUG_IOCTL_ARG);
  1150. #endif
  1151. return v4l2_fh_open(file);
  1152. }
  1153. static int dev_close(struct file *file)
  1154. {
  1155. struct gspca_dev *gspca_dev = video_drvdata(file);
  1156. PDEBUG(D_STREAM, "[%s] close", current->comm);
  1157. /* Needed for gspca_stream_off, always lock before queue_lock! */
  1158. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1159. return -ERESTARTSYS;
  1160. if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
  1161. mutex_unlock(&gspca_dev->usb_lock);
  1162. return -ERESTARTSYS;
  1163. }
  1164. /* if the file did the capture, free the streaming resources */
  1165. if (gspca_dev->capt_file == file) {
  1166. if (gspca_dev->streaming)
  1167. gspca_stream_off(gspca_dev);
  1168. frame_free(gspca_dev);
  1169. }
  1170. module_put(gspca_dev->module);
  1171. mutex_unlock(&gspca_dev->queue_lock);
  1172. mutex_unlock(&gspca_dev->usb_lock);
  1173. PDEBUG(D_STREAM, "close done");
  1174. return v4l2_fh_release(file);
  1175. }
  1176. static int vidioc_querycap(struct file *file, void *priv,
  1177. struct v4l2_capability *cap)
  1178. {
  1179. struct gspca_dev *gspca_dev = video_drvdata(file);
  1180. strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
  1181. sizeof cap->driver);
  1182. if (gspca_dev->dev->product != NULL) {
  1183. strlcpy((char *) cap->card, gspca_dev->dev->product,
  1184. sizeof cap->card);
  1185. } else {
  1186. snprintf((char *) cap->card, sizeof cap->card,
  1187. "USB Camera (%04x:%04x)",
  1188. le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
  1189. le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
  1190. }
  1191. usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
  1192. sizeof(cap->bus_info));
  1193. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  1194. | V4L2_CAP_STREAMING
  1195. | V4L2_CAP_READWRITE;
  1196. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1197. return 0;
  1198. }
  1199. static int get_ctrl(struct gspca_dev *gspca_dev,
  1200. int id)
  1201. {
  1202. const struct ctrl *ctrls;
  1203. int i;
  1204. for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
  1205. i < gspca_dev->sd_desc->nctrls;
  1206. i++, ctrls++) {
  1207. if (gspca_dev->ctrl_dis & (1 << i))
  1208. continue;
  1209. if (id == ctrls->qctrl.id)
  1210. return i;
  1211. }
  1212. return -1;
  1213. }
  1214. static int vidioc_queryctrl(struct file *file, void *priv,
  1215. struct v4l2_queryctrl *q_ctrl)
  1216. {
  1217. struct gspca_dev *gspca_dev = video_drvdata(file);
  1218. const struct ctrl *ctrls;
  1219. struct gspca_ctrl *gspca_ctrl;
  1220. int i, idx;
  1221. u32 id;
  1222. id = q_ctrl->id;
  1223. if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  1224. id &= V4L2_CTRL_ID_MASK;
  1225. id++;
  1226. idx = -1;
  1227. for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
  1228. if (gspca_dev->ctrl_dis & (1 << i))
  1229. continue;
  1230. if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
  1231. continue;
  1232. if (idx >= 0
  1233. && gspca_dev->sd_desc->ctrls[i].qctrl.id
  1234. > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
  1235. continue;
  1236. idx = i;
  1237. }
  1238. } else {
  1239. idx = get_ctrl(gspca_dev, id);
  1240. }
  1241. if (idx < 0)
  1242. return -EINVAL;
  1243. ctrls = &gspca_dev->sd_desc->ctrls[idx];
  1244. memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
  1245. if (gspca_dev->cam.ctrls != NULL) {
  1246. gspca_ctrl = &gspca_dev->cam.ctrls[idx];
  1247. q_ctrl->default_value = gspca_ctrl->def;
  1248. q_ctrl->minimum = gspca_ctrl->min;
  1249. q_ctrl->maximum = gspca_ctrl->max;
  1250. }
  1251. if (gspca_dev->ctrl_inac & (1 << idx))
  1252. q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
  1253. return 0;
  1254. }
  1255. static int vidioc_s_ctrl(struct file *file, void *priv,
  1256. struct v4l2_control *ctrl)
  1257. {
  1258. struct gspca_dev *gspca_dev = video_drvdata(file);
  1259. const struct ctrl *ctrls;
  1260. struct gspca_ctrl *gspca_ctrl;
  1261. int idx;
  1262. idx = get_ctrl(gspca_dev, ctrl->id);
  1263. if (idx < 0)
  1264. return -EINVAL;
  1265. if (gspca_dev->ctrl_inac & (1 << idx))
  1266. return -EINVAL;
  1267. ctrls = &gspca_dev->sd_desc->ctrls[idx];
  1268. if (gspca_dev->cam.ctrls != NULL) {
  1269. gspca_ctrl = &gspca_dev->cam.ctrls[idx];
  1270. if (ctrl->value < gspca_ctrl->min
  1271. || ctrl->value > gspca_ctrl->max)
  1272. return -ERANGE;
  1273. } else {
  1274. gspca_ctrl = NULL;
  1275. if (ctrl->value < ctrls->qctrl.minimum
  1276. || ctrl->value > ctrls->qctrl.maximum)
  1277. return -ERANGE;
  1278. }
  1279. PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
  1280. gspca_dev->usb_err = 0;
  1281. if (ctrls->set != NULL)
  1282. return ctrls->set(gspca_dev, ctrl->value);
  1283. if (gspca_ctrl != NULL) {
  1284. gspca_ctrl->val = ctrl->value;
  1285. if (ctrls->set_control != NULL
  1286. && gspca_dev->streaming)
  1287. ctrls->set_control(gspca_dev);
  1288. }
  1289. return gspca_dev->usb_err;
  1290. }
  1291. static int vidioc_g_ctrl(struct file *file, void *priv,
  1292. struct v4l2_control *ctrl)
  1293. {
  1294. struct gspca_dev *gspca_dev = video_drvdata(file);
  1295. const struct ctrl *ctrls;
  1296. int idx;
  1297. idx = get_ctrl(gspca_dev, ctrl->id);
  1298. if (idx < 0)
  1299. return -EINVAL;
  1300. ctrls = &gspca_dev->sd_desc->ctrls[idx];
  1301. gspca_dev->usb_err = 0;
  1302. if (ctrls->get != NULL)
  1303. return ctrls->get(gspca_dev, &ctrl->value);
  1304. if (gspca_dev->cam.ctrls != NULL)
  1305. ctrl->value = gspca_dev->cam.ctrls[idx].val;
  1306. return 0;
  1307. }
  1308. static int vidioc_querymenu(struct file *file, void *priv,
  1309. struct v4l2_querymenu *qmenu)
  1310. {
  1311. struct gspca_dev *gspca_dev = video_drvdata(file);
  1312. if (!gspca_dev->sd_desc->querymenu)
  1313. return -ENOTTY;
  1314. return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
  1315. }
  1316. static int vidioc_enum_input(struct file *file, void *priv,
  1317. struct v4l2_input *input)
  1318. {
  1319. struct gspca_dev *gspca_dev = video_drvdata(file);
  1320. if (input->index != 0)
  1321. return -EINVAL;
  1322. input->type = V4L2_INPUT_TYPE_CAMERA;
  1323. input->status = gspca_dev->cam.input_flags;
  1324. strlcpy(input->name, gspca_dev->sd_desc->name,
  1325. sizeof input->name);
  1326. return 0;
  1327. }
  1328. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1329. {
  1330. *i = 0;
  1331. return 0;
  1332. }
  1333. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1334. {
  1335. if (i > 0)
  1336. return -EINVAL;
  1337. return (0);
  1338. }
  1339. static int vidioc_reqbufs(struct file *file, void *priv,
  1340. struct v4l2_requestbuffers *rb)
  1341. {
  1342. struct gspca_dev *gspca_dev = video_drvdata(file);
  1343. int i, ret = 0, streaming;
  1344. i = rb->memory; /* (avoid compilation warning) */
  1345. switch (i) {
  1346. case GSPCA_MEMORY_READ: /* (internal call) */
  1347. case V4L2_MEMORY_MMAP:
  1348. case V4L2_MEMORY_USERPTR:
  1349. break;
  1350. default:
  1351. return -EINVAL;
  1352. }
  1353. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1354. return -ERESTARTSYS;
  1355. if (gspca_dev->memory != GSPCA_MEMORY_NO
  1356. && gspca_dev->memory != GSPCA_MEMORY_READ
  1357. && gspca_dev->memory != rb->memory) {
  1358. ret = -EBUSY;
  1359. goto out;
  1360. }
  1361. /* only one file may do the capture */
  1362. if (gspca_dev->capt_file != NULL
  1363. && gspca_dev->capt_file != file) {
  1364. ret = -EBUSY;
  1365. goto out;
  1366. }
  1367. /* if allocated, the buffers must not be mapped */
  1368. for (i = 0; i < gspca_dev->nframes; i++) {
  1369. if (gspca_dev->frame[i].vma_use_count) {
  1370. ret = -EBUSY;
  1371. goto out;
  1372. }
  1373. }
  1374. /* stop streaming */
  1375. streaming = gspca_dev->streaming;
  1376. if (streaming) {
  1377. gspca_stream_off(gspca_dev);
  1378. /* Don't restart the stream when switching from read
  1379. * to mmap mode */
  1380. if (gspca_dev->memory == GSPCA_MEMORY_READ)
  1381. streaming = 0;
  1382. }
  1383. /* free the previous allocated buffers, if any */
  1384. if (gspca_dev->nframes != 0)
  1385. frame_free(gspca_dev);
  1386. if (rb->count == 0) /* unrequest */
  1387. goto out;
  1388. ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
  1389. if (ret == 0) {
  1390. rb->count = gspca_dev->nframes;
  1391. if (streaming)
  1392. ret = gspca_init_transfer(gspca_dev);
  1393. }
  1394. out:
  1395. mutex_unlock(&gspca_dev->queue_lock);
  1396. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  1397. return ret;
  1398. }
  1399. static int vidioc_querybuf(struct file *file, void *priv,
  1400. struct v4l2_buffer *v4l2_buf)
  1401. {
  1402. struct gspca_dev *gspca_dev = video_drvdata(file);
  1403. struct gspca_frame *frame;
  1404. if (v4l2_buf->index >= gspca_dev->nframes)
  1405. return -EINVAL;
  1406. frame = &gspca_dev->frame[v4l2_buf->index];
  1407. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1408. return 0;
  1409. }
  1410. static int vidioc_streamon(struct file *file, void *priv,
  1411. enum v4l2_buf_type buf_type)
  1412. {
  1413. struct gspca_dev *gspca_dev = video_drvdata(file);
  1414. int ret;
  1415. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1416. return -EINVAL;
  1417. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1418. return -ERESTARTSYS;
  1419. /* check the capture file */
  1420. if (gspca_dev->capt_file != file) {
  1421. ret = -EBUSY;
  1422. goto out;
  1423. }
  1424. if (gspca_dev->nframes == 0
  1425. || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
  1426. ret = -EINVAL;
  1427. goto out;
  1428. }
  1429. if (!gspca_dev->streaming) {
  1430. ret = gspca_init_transfer(gspca_dev);
  1431. if (ret < 0)
  1432. goto out;
  1433. }
  1434. #ifdef GSPCA_DEBUG
  1435. if (gspca_debug & D_STREAM) {
  1436. PDEBUG_MODE("stream on OK",
  1437. gspca_dev->pixfmt,
  1438. gspca_dev->width,
  1439. gspca_dev->height);
  1440. }
  1441. #endif
  1442. ret = 0;
  1443. out:
  1444. mutex_unlock(&gspca_dev->queue_lock);
  1445. return ret;
  1446. }
  1447. static int vidioc_streamoff(struct file *file, void *priv,
  1448. enum v4l2_buf_type buf_type)
  1449. {
  1450. struct gspca_dev *gspca_dev = video_drvdata(file);
  1451. int i, ret;
  1452. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1453. return -EINVAL;
  1454. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1455. return -ERESTARTSYS;
  1456. if (!gspca_dev->streaming) {
  1457. ret = 0;
  1458. goto out;
  1459. }
  1460. /* check the capture file */
  1461. if (gspca_dev->capt_file != file) {
  1462. ret = -EBUSY;
  1463. goto out;
  1464. }
  1465. /* stop streaming */
  1466. gspca_stream_off(gspca_dev);
  1467. /* In case another thread is waiting in dqbuf */
  1468. wake_up_interruptible(&gspca_dev->wq);
  1469. /* empty the transfer queues */
  1470. for (i = 0; i < gspca_dev->nframes; i++)
  1471. gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
  1472. atomic_set(&gspca_dev->fr_q, 0);
  1473. atomic_set(&gspca_dev->fr_i, 0);
  1474. gspca_dev->fr_o = 0;
  1475. ret = 0;
  1476. out:
  1477. mutex_unlock(&gspca_dev->queue_lock);
  1478. return ret;
  1479. }
  1480. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1481. struct v4l2_jpegcompression *jpegcomp)
  1482. {
  1483. struct gspca_dev *gspca_dev = video_drvdata(file);
  1484. gspca_dev->usb_err = 0;
  1485. return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1486. }
  1487. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1488. const struct v4l2_jpegcompression *jpegcomp)
  1489. {
  1490. struct gspca_dev *gspca_dev = video_drvdata(file);
  1491. gspca_dev->usb_err = 0;
  1492. return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1493. }
  1494. static int vidioc_g_parm(struct file *filp, void *priv,
  1495. struct v4l2_streamparm *parm)
  1496. {
  1497. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1498. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1499. if (gspca_dev->sd_desc->get_streamparm) {
  1500. gspca_dev->usb_err = 0;
  1501. gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
  1502. return gspca_dev->usb_err;
  1503. }
  1504. return 0;
  1505. }
  1506. static int vidioc_s_parm(struct file *filp, void *priv,
  1507. struct v4l2_streamparm *parm)
  1508. {
  1509. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1510. int n;
  1511. n = parm->parm.capture.readbuffers;
  1512. if (n == 0 || n >= GSPCA_MAX_FRAMES)
  1513. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1514. else
  1515. gspca_dev->nbufread = n;
  1516. if (gspca_dev->sd_desc->set_streamparm) {
  1517. gspca_dev->usb_err = 0;
  1518. gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
  1519. return gspca_dev->usb_err;
  1520. }
  1521. return 0;
  1522. }
  1523. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1524. {
  1525. struct gspca_dev *gspca_dev = video_drvdata(file);
  1526. struct gspca_frame *frame;
  1527. struct page *page;
  1528. unsigned long addr, start, size;
  1529. int i, ret;
  1530. start = vma->vm_start;
  1531. size = vma->vm_end - vma->vm_start;
  1532. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1533. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1534. return -ERESTARTSYS;
  1535. if (gspca_dev->capt_file != file) {
  1536. ret = -EINVAL;
  1537. goto out;
  1538. }
  1539. frame = NULL;
  1540. for (i = 0; i < gspca_dev->nframes; ++i) {
  1541. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1542. PDEBUG(D_STREAM, "mmap bad memory type");
  1543. break;
  1544. }
  1545. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1546. == vma->vm_pgoff) {
  1547. frame = &gspca_dev->frame[i];
  1548. break;
  1549. }
  1550. }
  1551. if (frame == NULL) {
  1552. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1553. ret = -EINVAL;
  1554. goto out;
  1555. }
  1556. if (size != frame->v4l2_buf.length) {
  1557. PDEBUG(D_STREAM, "mmap bad size");
  1558. ret = -EINVAL;
  1559. goto out;
  1560. }
  1561. /*
  1562. * - VM_IO marks the area as being a mmaped region for I/O to a
  1563. * device. It also prevents the region from being core dumped.
  1564. */
  1565. vma->vm_flags |= VM_IO;
  1566. addr = (unsigned long) frame->data;
  1567. while (size > 0) {
  1568. page = vmalloc_to_page((void *) addr);
  1569. ret = vm_insert_page(vma, start, page);
  1570. if (ret < 0)
  1571. goto out;
  1572. start += PAGE_SIZE;
  1573. addr += PAGE_SIZE;
  1574. size -= PAGE_SIZE;
  1575. }
  1576. vma->vm_ops = &gspca_vm_ops;
  1577. vma->vm_private_data = frame;
  1578. gspca_vm_open(vma);
  1579. ret = 0;
  1580. out:
  1581. mutex_unlock(&gspca_dev->queue_lock);
  1582. return ret;
  1583. }
  1584. static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
  1585. enum v4l2_memory memory)
  1586. {
  1587. if (!gspca_dev->present)
  1588. return -ENODEV;
  1589. if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
  1590. !gspca_dev->streaming)
  1591. return -EINVAL;
  1592. /* check if a frame is ready */
  1593. return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
  1594. }
  1595. static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
  1596. enum v4l2_memory memory)
  1597. {
  1598. int ret;
  1599. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1600. return -ERESTARTSYS;
  1601. ret = frame_ready_nolock(gspca_dev, file, memory);
  1602. mutex_unlock(&gspca_dev->queue_lock);
  1603. return ret;
  1604. }
  1605. /*
  1606. * dequeue a video buffer
  1607. *
  1608. * If nonblock_ing is false, block until a buffer is available.
  1609. */
  1610. static int vidioc_dqbuf(struct file *file, void *priv,
  1611. struct v4l2_buffer *v4l2_buf)
  1612. {
  1613. struct gspca_dev *gspca_dev = video_drvdata(file);
  1614. struct gspca_frame *frame;
  1615. int i, j, ret;
  1616. PDEBUG(D_FRAM, "dqbuf");
  1617. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1618. return -ERESTARTSYS;
  1619. for (;;) {
  1620. ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
  1621. if (ret < 0)
  1622. goto out;
  1623. if (ret > 0)
  1624. break;
  1625. mutex_unlock(&gspca_dev->queue_lock);
  1626. if (file->f_flags & O_NONBLOCK)
  1627. return -EAGAIN;
  1628. /* wait till a frame is ready */
  1629. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1630. frame_ready(gspca_dev, file, v4l2_buf->memory),
  1631. msecs_to_jiffies(3000));
  1632. if (ret < 0)
  1633. return ret;
  1634. if (ret == 0)
  1635. return -EIO;
  1636. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1637. return -ERESTARTSYS;
  1638. }
  1639. i = gspca_dev->fr_o;
  1640. j = gspca_dev->fr_queue[i];
  1641. frame = &gspca_dev->frame[j];
  1642. gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
  1643. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1644. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1645. PDEBUG(D_FRAM, "dqbuf %d", j);
  1646. ret = 0;
  1647. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1648. if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
  1649. frame->data,
  1650. frame->v4l2_buf.bytesused)) {
  1651. PDEBUG(D_ERR|D_STREAM,
  1652. "dqbuf cp to user failed");
  1653. ret = -EFAULT;
  1654. }
  1655. }
  1656. out:
  1657. mutex_unlock(&gspca_dev->queue_lock);
  1658. if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
  1659. mutex_lock(&gspca_dev->usb_lock);
  1660. gspca_dev->usb_err = 0;
  1661. if (gspca_dev->present)
  1662. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1663. mutex_unlock(&gspca_dev->usb_lock);
  1664. }
  1665. return ret;
  1666. }
  1667. /*
  1668. * queue a video buffer
  1669. *
  1670. * Attempting to queue a buffer that has already been
  1671. * queued will return -EINVAL.
  1672. */
  1673. static int vidioc_qbuf(struct file *file, void *priv,
  1674. struct v4l2_buffer *v4l2_buf)
  1675. {
  1676. struct gspca_dev *gspca_dev = video_drvdata(file);
  1677. struct gspca_frame *frame;
  1678. int i, index, ret;
  1679. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1680. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1681. return -ERESTARTSYS;
  1682. index = v4l2_buf->index;
  1683. if ((unsigned) index >= gspca_dev->nframes) {
  1684. PDEBUG(D_FRAM,
  1685. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1686. ret = -EINVAL;
  1687. goto out;
  1688. }
  1689. if (v4l2_buf->memory != gspca_dev->memory) {
  1690. PDEBUG(D_FRAM, "qbuf bad memory type");
  1691. ret = -EINVAL;
  1692. goto out;
  1693. }
  1694. frame = &gspca_dev->frame[index];
  1695. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1696. PDEBUG(D_FRAM, "qbuf bad state");
  1697. ret = -EINVAL;
  1698. goto out;
  1699. }
  1700. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1701. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1702. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1703. frame->v4l2_buf.length = v4l2_buf->length;
  1704. }
  1705. /* put the buffer in the 'queued' queue */
  1706. i = atomic_read(&gspca_dev->fr_q);
  1707. gspca_dev->fr_queue[i] = index;
  1708. atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
  1709. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1710. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1711. ret = 0;
  1712. out:
  1713. mutex_unlock(&gspca_dev->queue_lock);
  1714. return ret;
  1715. }
  1716. /*
  1717. * allocate the resources for read()
  1718. */
  1719. static int read_alloc(struct gspca_dev *gspca_dev,
  1720. struct file *file)
  1721. {
  1722. struct v4l2_buffer v4l2_buf;
  1723. int i, ret;
  1724. PDEBUG(D_STREAM, "read alloc");
  1725. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1726. return -ERESTARTSYS;
  1727. if (gspca_dev->nframes == 0) {
  1728. struct v4l2_requestbuffers rb;
  1729. memset(&rb, 0, sizeof rb);
  1730. rb.count = gspca_dev->nbufread;
  1731. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1732. rb.memory = GSPCA_MEMORY_READ;
  1733. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1734. if (ret != 0) {
  1735. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1736. goto out;
  1737. }
  1738. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1739. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1740. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1741. for (i = 0; i < gspca_dev->nbufread; i++) {
  1742. v4l2_buf.index = i;
  1743. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1744. if (ret != 0) {
  1745. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1746. goto out;
  1747. }
  1748. }
  1749. }
  1750. /* start streaming */
  1751. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1752. if (ret != 0)
  1753. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1754. out:
  1755. mutex_unlock(&gspca_dev->usb_lock);
  1756. return ret;
  1757. }
  1758. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1759. {
  1760. struct gspca_dev *gspca_dev = video_drvdata(file);
  1761. unsigned long req_events = poll_requested_events(wait);
  1762. int ret = 0;
  1763. PDEBUG(D_FRAM, "poll");
  1764. if (req_events & POLLPRI)
  1765. ret |= v4l2_ctrl_poll(file, wait);
  1766. if (req_events & (POLLIN | POLLRDNORM)) {
  1767. /* if reqbufs is not done, the user would use read() */
  1768. if (gspca_dev->memory == GSPCA_MEMORY_NO) {
  1769. if (read_alloc(gspca_dev, file) != 0) {
  1770. ret |= POLLERR;
  1771. goto out;
  1772. }
  1773. }
  1774. poll_wait(file, &gspca_dev->wq, wait);
  1775. /* check if an image has been received */
  1776. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
  1777. ret |= POLLERR;
  1778. goto out;
  1779. }
  1780. if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
  1781. ret |= POLLIN | POLLRDNORM;
  1782. mutex_unlock(&gspca_dev->queue_lock);
  1783. }
  1784. out:
  1785. if (!gspca_dev->present)
  1786. ret |= POLLHUP;
  1787. return ret;
  1788. }
  1789. static ssize_t dev_read(struct file *file, char __user *data,
  1790. size_t count, loff_t *ppos)
  1791. {
  1792. struct gspca_dev *gspca_dev = video_drvdata(file);
  1793. struct gspca_frame *frame;
  1794. struct v4l2_buffer v4l2_buf;
  1795. struct timeval timestamp;
  1796. int n, ret, ret2;
  1797. PDEBUG(D_FRAM, "read (%zd)", count);
  1798. if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
  1799. ret = read_alloc(gspca_dev, file);
  1800. if (ret != 0)
  1801. return ret;
  1802. }
  1803. /* get a frame */
  1804. timestamp = ktime_to_timeval(ktime_get());
  1805. timestamp.tv_sec--;
  1806. n = 2;
  1807. for (;;) {
  1808. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1809. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1810. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1811. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1812. if (ret != 0) {
  1813. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1814. return ret;
  1815. }
  1816. /* if the process slept for more than 1 second,
  1817. * get a newer frame */
  1818. frame = &gspca_dev->frame[v4l2_buf.index];
  1819. if (--n < 0)
  1820. break; /* avoid infinite loop */
  1821. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1822. break;
  1823. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1824. if (ret != 0) {
  1825. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1826. return ret;
  1827. }
  1828. }
  1829. /* copy the frame */
  1830. if (count > frame->v4l2_buf.bytesused)
  1831. count = frame->v4l2_buf.bytesused;
  1832. ret = copy_to_user(data, frame->data, count);
  1833. if (ret != 0) {
  1834. PDEBUG(D_ERR|D_STREAM,
  1835. "read cp to user lack %d / %zd", ret, count);
  1836. ret = -EFAULT;
  1837. goto out;
  1838. }
  1839. ret = count;
  1840. out:
  1841. /* in each case, requeue the buffer */
  1842. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1843. if (ret2 != 0)
  1844. return ret2;
  1845. return ret;
  1846. }
  1847. static struct v4l2_file_operations dev_fops = {
  1848. .owner = THIS_MODULE,
  1849. .open = dev_open,
  1850. .release = dev_close,
  1851. .read = dev_read,
  1852. .mmap = dev_mmap,
  1853. .unlocked_ioctl = video_ioctl2,
  1854. .poll = dev_poll,
  1855. };
  1856. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1857. .vidioc_querycap = vidioc_querycap,
  1858. .vidioc_dqbuf = vidioc_dqbuf,
  1859. .vidioc_qbuf = vidioc_qbuf,
  1860. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1861. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1862. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1863. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1864. .vidioc_streamon = vidioc_streamon,
  1865. .vidioc_queryctrl = vidioc_queryctrl,
  1866. .vidioc_g_ctrl = vidioc_g_ctrl,
  1867. .vidioc_s_ctrl = vidioc_s_ctrl,
  1868. .vidioc_querymenu = vidioc_querymenu,
  1869. .vidioc_enum_input = vidioc_enum_input,
  1870. .vidioc_g_input = vidioc_g_input,
  1871. .vidioc_s_input = vidioc_s_input,
  1872. .vidioc_reqbufs = vidioc_reqbufs,
  1873. .vidioc_querybuf = vidioc_querybuf,
  1874. .vidioc_streamoff = vidioc_streamoff,
  1875. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1876. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1877. .vidioc_g_parm = vidioc_g_parm,
  1878. .vidioc_s_parm = vidioc_s_parm,
  1879. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1880. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  1881. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1882. .vidioc_g_register = vidioc_g_register,
  1883. .vidioc_s_register = vidioc_s_register,
  1884. #endif
  1885. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1886. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1887. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1888. };
  1889. static const struct video_device gspca_template = {
  1890. .name = "gspca main driver",
  1891. .fops = &dev_fops,
  1892. .ioctl_ops = &dev_ioctl_ops,
  1893. .release = video_device_release_empty, /* We use v4l2_dev.release */
  1894. };
  1895. /* initialize the controls */
  1896. static void ctrls_init(struct gspca_dev *gspca_dev)
  1897. {
  1898. struct gspca_ctrl *ctrl;
  1899. int i;
  1900. for (i = 0, ctrl = gspca_dev->cam.ctrls;
  1901. i < gspca_dev->sd_desc->nctrls;
  1902. i++, ctrl++) {
  1903. ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
  1904. ctrl->val = ctrl->def;
  1905. ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
  1906. ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
  1907. }
  1908. }
  1909. /*
  1910. * probe and create a new gspca device
  1911. *
  1912. * This function must be called by the sub-driver when it is
  1913. * called for probing a new device.
  1914. */
  1915. int gspca_dev_probe2(struct usb_interface *intf,
  1916. const struct usb_device_id *id,
  1917. const struct sd_desc *sd_desc,
  1918. int dev_size,
  1919. struct module *module)
  1920. {
  1921. struct gspca_dev *gspca_dev;
  1922. struct usb_device *dev = interface_to_usbdev(intf);
  1923. int ret;
  1924. pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
  1925. sd_desc->name, id->idVendor, id->idProduct);
  1926. /* create the device */
  1927. if (dev_size < sizeof *gspca_dev)
  1928. dev_size = sizeof *gspca_dev;
  1929. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1930. if (!gspca_dev) {
  1931. pr_err("couldn't kzalloc gspca struct\n");
  1932. return -ENOMEM;
  1933. }
  1934. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1935. if (!gspca_dev->usb_buf) {
  1936. pr_err("out of memory\n");
  1937. ret = -ENOMEM;
  1938. goto out;
  1939. }
  1940. gspca_dev->dev = dev;
  1941. gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
  1942. /* check if any audio device */
  1943. if (dev->actconfig->desc.bNumInterfaces != 1) {
  1944. int i;
  1945. struct usb_interface *intf2;
  1946. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1947. intf2 = dev->actconfig->interface[i];
  1948. if (intf2 != NULL
  1949. && intf2->altsetting != NULL
  1950. && intf2->altsetting->desc.bInterfaceClass ==
  1951. USB_CLASS_AUDIO) {
  1952. gspca_dev->audio = 1;
  1953. break;
  1954. }
  1955. }
  1956. }
  1957. gspca_dev->v4l2_dev.release = gspca_release;
  1958. ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
  1959. if (ret)
  1960. goto out;
  1961. gspca_dev->sd_desc = sd_desc;
  1962. gspca_dev->nbufread = 2;
  1963. gspca_dev->empty_packet = -1; /* don't check the empty packets */
  1964. gspca_dev->vdev = gspca_template;
  1965. gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
  1966. video_set_drvdata(&gspca_dev->vdev, gspca_dev);
  1967. set_bit(V4L2_FL_USE_FH_PRIO, &gspca_dev->vdev.flags);
  1968. gspca_dev->module = module;
  1969. gspca_dev->present = 1;
  1970. mutex_init(&gspca_dev->usb_lock);
  1971. gspca_dev->vdev.lock = &gspca_dev->usb_lock;
  1972. mutex_init(&gspca_dev->queue_lock);
  1973. init_waitqueue_head(&gspca_dev->wq);
  1974. /* configure the subdriver and initialize the USB device */
  1975. ret = sd_desc->config(gspca_dev, id);
  1976. if (ret < 0)
  1977. goto out;
  1978. if (gspca_dev->cam.ctrls != NULL)
  1979. ctrls_init(gspca_dev);
  1980. ret = sd_desc->init(gspca_dev);
  1981. if (ret < 0)
  1982. goto out;
  1983. if (sd_desc->init_controls)
  1984. ret = sd_desc->init_controls(gspca_dev);
  1985. if (ret < 0)
  1986. goto out;
  1987. gspca_set_default_mode(gspca_dev);
  1988. ret = gspca_input_connect(gspca_dev);
  1989. if (ret)
  1990. goto out;
  1991. /*
  1992. * Don't take usb_lock for these ioctls. This improves latency if
  1993. * usb_lock is taken for a long time, e.g. when changing a control
  1994. * value, and a new frame is ready to be dequeued.
  1995. */
  1996. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
  1997. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
  1998. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
  1999. if (!gspca_dev->sd_desc->get_chip_ident)
  2000. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_CHIP_IDENT);
  2001. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2002. if (!gspca_dev->sd_desc->get_chip_ident ||
  2003. !gspca_dev->sd_desc->get_register)
  2004. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
  2005. if (!gspca_dev->sd_desc->get_chip_ident ||
  2006. !gspca_dev->sd_desc->set_register)
  2007. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
  2008. #endif
  2009. if (!gspca_dev->sd_desc->get_jcomp)
  2010. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
  2011. if (!gspca_dev->sd_desc->set_jcomp)
  2012. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
  2013. /* init video stuff */
  2014. ret = video_register_device(&gspca_dev->vdev,
  2015. VFL_TYPE_GRABBER,
  2016. -1);
  2017. if (ret < 0) {
  2018. pr_err("video_register_device err %d\n", ret);
  2019. goto out;
  2020. }
  2021. usb_set_intfdata(intf, gspca_dev);
  2022. PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
  2023. gspca_input_create_urb(gspca_dev);
  2024. return 0;
  2025. out:
  2026. #if IS_ENABLED(CONFIG_INPUT)
  2027. if (gspca_dev->input_dev)
  2028. input_unregister_device(gspca_dev->input_dev);
  2029. #endif
  2030. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  2031. kfree(gspca_dev->usb_buf);
  2032. kfree(gspca_dev);
  2033. return ret;
  2034. }
  2035. EXPORT_SYMBOL(gspca_dev_probe2);
  2036. /* same function as the previous one, but check the interface */
  2037. int gspca_dev_probe(struct usb_interface *intf,
  2038. const struct usb_device_id *id,
  2039. const struct sd_desc *sd_desc,
  2040. int dev_size,
  2041. struct module *module)
  2042. {
  2043. struct usb_device *dev = interface_to_usbdev(intf);
  2044. /* we don't handle multi-config cameras */
  2045. if (dev->descriptor.bNumConfigurations != 1) {
  2046. pr_err("%04x:%04x too many config\n",
  2047. id->idVendor, id->idProduct);
  2048. return -ENODEV;
  2049. }
  2050. /* the USB video interface must be the first one */
  2051. if (dev->actconfig->desc.bNumInterfaces != 1
  2052. && intf->cur_altsetting->desc.bInterfaceNumber != 0)
  2053. return -ENODEV;
  2054. return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
  2055. }
  2056. EXPORT_SYMBOL(gspca_dev_probe);
  2057. /*
  2058. * USB disconnection
  2059. *
  2060. * This function must be called by the sub-driver
  2061. * when the device disconnects, after the specific resources are freed.
  2062. */
  2063. void gspca_disconnect(struct usb_interface *intf)
  2064. {
  2065. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2066. #if IS_ENABLED(CONFIG_INPUT)
  2067. struct input_dev *input_dev;
  2068. #endif
  2069. PDEBUG(D_PROBE, "%s disconnect",
  2070. video_device_node_name(&gspca_dev->vdev));
  2071. mutex_lock(&gspca_dev->usb_lock);
  2072. gspca_dev->present = 0;
  2073. destroy_urbs(gspca_dev);
  2074. #if IS_ENABLED(CONFIG_INPUT)
  2075. gspca_input_destroy_urb(gspca_dev);
  2076. input_dev = gspca_dev->input_dev;
  2077. if (input_dev) {
  2078. gspca_dev->input_dev = NULL;
  2079. input_unregister_device(input_dev);
  2080. }
  2081. #endif
  2082. /* Free subdriver's streaming resources / stop sd workqueue(s) */
  2083. if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
  2084. gspca_dev->sd_desc->stop0(gspca_dev);
  2085. gspca_dev->streaming = 0;
  2086. gspca_dev->dev = NULL;
  2087. wake_up_interruptible(&gspca_dev->wq);
  2088. v4l2_device_disconnect(&gspca_dev->v4l2_dev);
  2089. video_unregister_device(&gspca_dev->vdev);
  2090. mutex_unlock(&gspca_dev->usb_lock);
  2091. /* (this will call gspca_release() immediately or on last close) */
  2092. v4l2_device_put(&gspca_dev->v4l2_dev);
  2093. }
  2094. EXPORT_SYMBOL(gspca_disconnect);
  2095. #ifdef CONFIG_PM
  2096. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  2097. {
  2098. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2099. gspca_input_destroy_urb(gspca_dev);
  2100. if (!gspca_dev->streaming)
  2101. return 0;
  2102. mutex_lock(&gspca_dev->usb_lock);
  2103. gspca_dev->frozen = 1; /* avoid urb error messages */
  2104. gspca_dev->usb_err = 0;
  2105. if (gspca_dev->sd_desc->stopN)
  2106. gspca_dev->sd_desc->stopN(gspca_dev);
  2107. destroy_urbs(gspca_dev);
  2108. gspca_set_alt0(gspca_dev);
  2109. if (gspca_dev->sd_desc->stop0)
  2110. gspca_dev->sd_desc->stop0(gspca_dev);
  2111. mutex_unlock(&gspca_dev->usb_lock);
  2112. return 0;
  2113. }
  2114. EXPORT_SYMBOL(gspca_suspend);
  2115. int gspca_resume(struct usb_interface *intf)
  2116. {
  2117. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  2118. int streaming, ret = 0;
  2119. mutex_lock(&gspca_dev->usb_lock);
  2120. gspca_dev->frozen = 0;
  2121. gspca_dev->usb_err = 0;
  2122. gspca_dev->sd_desc->init(gspca_dev);
  2123. /*
  2124. * Most subdrivers send all ctrl values on sd_start and thus
  2125. * only write to the device registers on s_ctrl when streaming ->
  2126. * Clear streaming to avoid setting all ctrls twice.
  2127. */
  2128. streaming = gspca_dev->streaming;
  2129. gspca_dev->streaming = 0;
  2130. if (streaming)
  2131. ret = gspca_init_transfer(gspca_dev);
  2132. else
  2133. gspca_input_create_urb(gspca_dev);
  2134. mutex_unlock(&gspca_dev->usb_lock);
  2135. return ret;
  2136. }
  2137. EXPORT_SYMBOL(gspca_resume);
  2138. #endif
  2139. /* -- module insert / remove -- */
  2140. static int __init gspca_init(void)
  2141. {
  2142. pr_info("v" GSPCA_VERSION " registered\n");
  2143. return 0;
  2144. }
  2145. static void __exit gspca_exit(void)
  2146. {
  2147. }
  2148. module_init(gspca_init);
  2149. module_exit(gspca_exit);
  2150. #ifdef GSPCA_DEBUG
  2151. module_param_named(debug, gspca_debug, int, 0644);
  2152. MODULE_PARM_DESC(debug,
  2153. "Debug (bit) 0x01:error 0x02:probe 0x04:config"
  2154. " 0x08:stream 0x10:frame 0x20:packet"
  2155. " 0x0100: v4l2");
  2156. #endif