gspca.c 56 KB

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