gspca.c 56 KB

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