gspca.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267
  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. usb_free_coherent(gspca_dev->dev,
  505. urb->transfer_buffer_length,
  506. urb->transfer_buffer,
  507. urb->transfer_dma);
  508. usb_free_urb(urb);
  509. }
  510. }
  511. static int gspca_set_alt0(struct gspca_dev *gspca_dev)
  512. {
  513. int ret;
  514. if (gspca_dev->alt == 0)
  515. return 0;
  516. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
  517. if (ret < 0)
  518. pr_err("set alt 0 err %d\n", ret);
  519. return ret;
  520. }
  521. /* Note: both the queue and the usb locks should be held when calling this */
  522. static void gspca_stream_off(struct gspca_dev *gspca_dev)
  523. {
  524. gspca_dev->streaming = 0;
  525. gspca_dev->usb_err = 0;
  526. if (gspca_dev->sd_desc->stopN)
  527. gspca_dev->sd_desc->stopN(gspca_dev);
  528. destroy_urbs(gspca_dev);
  529. gspca_input_destroy_urb(gspca_dev);
  530. gspca_set_alt0(gspca_dev);
  531. gspca_input_create_urb(gspca_dev);
  532. if (gspca_dev->sd_desc->stop0)
  533. gspca_dev->sd_desc->stop0(gspca_dev);
  534. PDEBUG(D_STREAM, "stream off OK");
  535. }
  536. /*
  537. * look for an input transfer endpoint in an alternate setting
  538. */
  539. static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
  540. int xfer)
  541. {
  542. struct usb_host_endpoint *ep;
  543. int i, attr;
  544. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  545. ep = &alt->endpoint[i];
  546. attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  547. if (attr == xfer
  548. && ep->desc.wMaxPacketSize != 0
  549. && usb_endpoint_dir_in(&ep->desc))
  550. return ep;
  551. }
  552. return NULL;
  553. }
  554. /* compute the minimum bandwidth for the current transfer */
  555. static u32 which_bandwidth(struct gspca_dev *gspca_dev)
  556. {
  557. u32 bandwidth;
  558. int i;
  559. /* get the (max) image size */
  560. i = gspca_dev->curr_mode;
  561. bandwidth = gspca_dev->cam.cam_mode[i].sizeimage;
  562. /* if the image is compressed, estimate its mean size */
  563. if (!gspca_dev->cam.needs_full_bandwidth &&
  564. bandwidth < gspca_dev->cam.cam_mode[i].width *
  565. gspca_dev->cam.cam_mode[i].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->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. if (ep == NULL)
  615. continue;
  616. if (ep->desc.bInterval == 0) {
  617. pr_err("alt %d iso endp with 0 interval\n", j);
  618. continue;
  619. }
  620. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  621. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  622. bandwidth = psize * 1000;
  623. if (gspca_dev->dev->speed == USB_SPEED_HIGH
  624. || gspca_dev->dev->speed == USB_SPEED_SUPER)
  625. bandwidth *= 8;
  626. bandwidth /= 1 << (ep->desc.bInterval - 1);
  627. if (bandwidth <= last_bw)
  628. continue;
  629. if (bandwidth < ep_tb->bandwidth) {
  630. ep_tb->bandwidth = bandwidth;
  631. ep_tb->alt = j;
  632. found = 1;
  633. }
  634. }
  635. if (!found)
  636. break;
  637. PDEBUG(D_STREAM, "alt %d bandwidth %d",
  638. ep_tb->alt, ep_tb->bandwidth);
  639. last_bw = ep_tb->bandwidth;
  640. i++;
  641. ep_tb++;
  642. }
  643. /*
  644. * If the camera:
  645. * has a usb audio class interface (a built in usb mic); and
  646. * is a usb 1 full speed device; and
  647. * uses the max full speed iso bandwidth; and
  648. * and has more than 1 alt setting
  649. * then skip the highest alt setting to spare bandwidth for the mic
  650. */
  651. if (gspca_dev->audio &&
  652. gspca_dev->dev->speed == USB_SPEED_FULL &&
  653. last_bw >= 1000000 &&
  654. i > 1) {
  655. PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
  656. i--;
  657. ep_tb--;
  658. }
  659. /* get the requested bandwidth and start at the highest atlsetting */
  660. bandwidth = which_bandwidth(gspca_dev);
  661. ep_tb--;
  662. while (i > 1) {
  663. ep_tb--;
  664. if (ep_tb->bandwidth < bandwidth)
  665. break;
  666. i--;
  667. }
  668. return i;
  669. }
  670. /*
  671. * create the URBs for image transfer
  672. */
  673. static int create_urbs(struct gspca_dev *gspca_dev,
  674. struct usb_host_endpoint *ep)
  675. {
  676. struct urb *urb;
  677. int n, nurbs, i, psize, npkt, bsize;
  678. /* calculate the packet size and the number of packets */
  679. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  680. if (!gspca_dev->cam.bulk) { /* isoc */
  681. /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
  682. if (gspca_dev->pkt_size == 0)
  683. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  684. else
  685. psize = gspca_dev->pkt_size;
  686. npkt = gspca_dev->cam.npkt;
  687. if (npkt == 0)
  688. npkt = 32; /* default value */
  689. bsize = psize * npkt;
  690. PDEBUG(D_STREAM,
  691. "isoc %d pkts size %d = bsize:%d",
  692. npkt, psize, bsize);
  693. nurbs = DEF_NURBS;
  694. } else { /* bulk */
  695. npkt = 0;
  696. bsize = gspca_dev->cam.bulk_size;
  697. if (bsize == 0)
  698. bsize = psize;
  699. PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
  700. if (gspca_dev->cam.bulk_nurbs != 0)
  701. nurbs = gspca_dev->cam.bulk_nurbs;
  702. else
  703. nurbs = 1;
  704. }
  705. for (n = 0; n < nurbs; n++) {
  706. urb = usb_alloc_urb(npkt, GFP_KERNEL);
  707. if (!urb) {
  708. pr_err("usb_alloc_urb failed\n");
  709. return -ENOMEM;
  710. }
  711. gspca_dev->urb[n] = urb;
  712. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  713. bsize,
  714. GFP_KERNEL,
  715. &urb->transfer_dma);
  716. if (urb->transfer_buffer == NULL) {
  717. pr_err("usb_alloc_coherent failed\n");
  718. return -ENOMEM;
  719. }
  720. urb->dev = gspca_dev->dev;
  721. urb->context = gspca_dev;
  722. urb->transfer_buffer_length = bsize;
  723. if (npkt != 0) { /* ISOC */
  724. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  725. ep->desc.bEndpointAddress);
  726. urb->transfer_flags = URB_ISO_ASAP
  727. | URB_NO_TRANSFER_DMA_MAP;
  728. urb->interval = 1 << (ep->desc.bInterval - 1);
  729. urb->complete = isoc_irq;
  730. urb->number_of_packets = npkt;
  731. for (i = 0; i < npkt; i++) {
  732. urb->iso_frame_desc[i].length = psize;
  733. urb->iso_frame_desc[i].offset = psize * i;
  734. }
  735. } else { /* bulk */
  736. urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
  737. ep->desc.bEndpointAddress);
  738. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  739. urb->complete = bulk_irq;
  740. }
  741. }
  742. return 0;
  743. }
  744. /*
  745. * start the USB transfer
  746. */
  747. static int gspca_init_transfer(struct gspca_dev *gspca_dev)
  748. {
  749. struct usb_interface *intf;
  750. struct usb_host_endpoint *ep;
  751. struct urb *urb;
  752. struct ep_tb_s ep_tb[MAX_ALT];
  753. int n, ret, xfer, alt, alt_idx;
  754. /* reset the streaming variables */
  755. gspca_dev->image = NULL;
  756. gspca_dev->image_len = 0;
  757. gspca_dev->last_packet_type = DISCARD_PACKET;
  758. gspca_dev->sequence = 0;
  759. gspca_dev->usb_err = 0;
  760. /* do the specific subdriver stuff before endpoint selection */
  761. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  762. gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
  763. if (gspca_dev->sd_desc->isoc_init) {
  764. ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
  765. if (ret < 0)
  766. return ret;
  767. }
  768. xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
  769. : USB_ENDPOINT_XFER_ISOC;
  770. /* if bulk or the subdriver forced an altsetting, get the endpoint */
  771. if (gspca_dev->alt != 0) {
  772. gspca_dev->alt--; /* (previous version compatibility) */
  773. ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer);
  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. if (ret < 0) {
  814. destroy_urbs(gspca_dev);
  815. goto out;
  816. }
  817. }
  818. /* clear the bulk endpoint */
  819. if (gspca_dev->cam.bulk)
  820. usb_clear_halt(gspca_dev->dev,
  821. gspca_dev->urb[0]->pipe);
  822. /* start the cam */
  823. ret = gspca_dev->sd_desc->start(gspca_dev);
  824. if (ret < 0) {
  825. destroy_urbs(gspca_dev);
  826. goto out;
  827. }
  828. gspca_dev->streaming = 1;
  829. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  830. /* some bulk transfers are started by the subdriver */
  831. if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
  832. break;
  833. /* submit the URBs */
  834. for (n = 0; n < MAX_NURBS; n++) {
  835. urb = gspca_dev->urb[n];
  836. if (urb == NULL)
  837. break;
  838. ret = usb_submit_urb(urb, GFP_KERNEL);
  839. if (ret < 0)
  840. break;
  841. }
  842. if (ret >= 0)
  843. break; /* transfer is started */
  844. /* something when wrong
  845. * stop the webcam and free the transfer resources */
  846. gspca_stream_off(gspca_dev);
  847. if (ret != -ENOSPC) {
  848. pr_err("usb_submit_urb alt %d err %d\n",
  849. gspca_dev->alt, ret);
  850. goto out;
  851. }
  852. /* the bandwidth is not wide enough
  853. * negotiate or try a lower alternate setting */
  854. retry:
  855. PERR("alt %d - bandwidth not wide enough, trying again", alt);
  856. msleep(20); /* wait for kill complete */
  857. if (gspca_dev->sd_desc->isoc_nego) {
  858. ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
  859. if (ret < 0)
  860. goto out;
  861. } else {
  862. if (alt_idx <= 0) {
  863. pr_err("no transfer endpoint found\n");
  864. ret = -EIO;
  865. goto out;
  866. }
  867. gspca_dev->alt = ep_tb[--alt_idx].alt;
  868. }
  869. }
  870. out:
  871. gspca_input_create_urb(gspca_dev);
  872. return ret;
  873. }
  874. static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
  875. {
  876. int i;
  877. i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
  878. gspca_dev->curr_mode = i;
  879. gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
  880. gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
  881. gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
  882. /* does nothing if ctrl_handler == NULL */
  883. v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
  884. }
  885. static int wxh_to_mode(struct gspca_dev *gspca_dev,
  886. int width, int height)
  887. {
  888. int i;
  889. for (i = gspca_dev->cam.nmodes; --i > 0; ) {
  890. if (width >= gspca_dev->cam.cam_mode[i].width
  891. && height >= gspca_dev->cam.cam_mode[i].height)
  892. break;
  893. }
  894. return i;
  895. }
  896. /*
  897. * search a mode with the right pixel format
  898. */
  899. static int gspca_get_mode(struct gspca_dev *gspca_dev,
  900. int mode,
  901. int pixfmt)
  902. {
  903. int modeU, modeD;
  904. modeU = modeD = mode;
  905. while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
  906. if (--modeD >= 0) {
  907. if (gspca_dev->cam.cam_mode[modeD].pixelformat
  908. == pixfmt)
  909. return modeD;
  910. }
  911. if (++modeU < gspca_dev->cam.nmodes) {
  912. if (gspca_dev->cam.cam_mode[modeU].pixelformat
  913. == pixfmt)
  914. return modeU;
  915. }
  916. }
  917. return -EINVAL;
  918. }
  919. #ifdef CONFIG_VIDEO_ADV_DEBUG
  920. static int vidioc_g_chip_info(struct file *file, void *priv,
  921. struct v4l2_dbg_chip_info *chip)
  922. {
  923. struct gspca_dev *gspca_dev = video_drvdata(file);
  924. gspca_dev->usb_err = 0;
  925. if (gspca_dev->sd_desc->get_chip_info)
  926. return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip);
  927. return chip->match.addr ? -EINVAL : 0;
  928. }
  929. static int vidioc_g_register(struct file *file, void *priv,
  930. struct v4l2_dbg_register *reg)
  931. {
  932. struct gspca_dev *gspca_dev = video_drvdata(file);
  933. gspca_dev->usb_err = 0;
  934. return gspca_dev->sd_desc->get_register(gspca_dev, reg);
  935. }
  936. static int vidioc_s_register(struct file *file, void *priv,
  937. const struct v4l2_dbg_register *reg)
  938. {
  939. struct gspca_dev *gspca_dev = video_drvdata(file);
  940. gspca_dev->usb_err = 0;
  941. return gspca_dev->sd_desc->set_register(gspca_dev, reg);
  942. }
  943. #endif
  944. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  945. struct v4l2_fmtdesc *fmtdesc)
  946. {
  947. struct gspca_dev *gspca_dev = video_drvdata(file);
  948. int i, j, index;
  949. __u32 fmt_tb[8];
  950. /* give an index to each format */
  951. index = 0;
  952. j = 0;
  953. for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
  954. fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
  955. j = 0;
  956. for (;;) {
  957. if (fmt_tb[j] == fmt_tb[index])
  958. break;
  959. j++;
  960. }
  961. if (j == index) {
  962. if (fmtdesc->index == index)
  963. break; /* new format */
  964. index++;
  965. if (index >= ARRAY_SIZE(fmt_tb))
  966. return -EINVAL;
  967. }
  968. }
  969. if (i < 0)
  970. return -EINVAL; /* no more format */
  971. fmtdesc->pixelformat = fmt_tb[index];
  972. if (gspca_dev->cam.cam_mode[i].sizeimage <
  973. gspca_dev->cam.cam_mode[i].width *
  974. gspca_dev->cam.cam_mode[i].height)
  975. fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
  976. fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
  977. fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
  978. fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
  979. fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
  980. fmtdesc->description[4] = '\0';
  981. return 0;
  982. }
  983. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  984. struct v4l2_format *fmt)
  985. {
  986. struct gspca_dev *gspca_dev = video_drvdata(file);
  987. int mode;
  988. mode = gspca_dev->curr_mode;
  989. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  990. /* some drivers use priv internally, zero it before giving it to
  991. userspace */
  992. fmt->fmt.pix.priv = 0;
  993. return 0;
  994. }
  995. static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
  996. struct v4l2_format *fmt)
  997. {
  998. int w, h, mode, mode2;
  999. w = fmt->fmt.pix.width;
  1000. h = fmt->fmt.pix.height;
  1001. PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap",
  1002. fmt->fmt.pix.pixelformat, w, h);
  1003. /* search the closest mode for width and height */
  1004. mode = wxh_to_mode(gspca_dev, w, h);
  1005. /* OK if right palette */
  1006. if (gspca_dev->cam.cam_mode[mode].pixelformat
  1007. != fmt->fmt.pix.pixelformat) {
  1008. /* else, search the closest mode with the same pixel format */
  1009. mode2 = gspca_get_mode(gspca_dev, mode,
  1010. fmt->fmt.pix.pixelformat);
  1011. if (mode2 >= 0)
  1012. mode = mode2;
  1013. }
  1014. fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
  1015. /* some drivers use priv internally, zero it before giving it to
  1016. userspace */
  1017. fmt->fmt.pix.priv = 0;
  1018. return mode; /* used when s_fmt */
  1019. }
  1020. static int vidioc_try_fmt_vid_cap(struct file *file,
  1021. void *priv,
  1022. struct v4l2_format *fmt)
  1023. {
  1024. struct gspca_dev *gspca_dev = video_drvdata(file);
  1025. int ret;
  1026. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1027. if (ret < 0)
  1028. return ret;
  1029. return 0;
  1030. }
  1031. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1032. struct v4l2_format *fmt)
  1033. {
  1034. struct gspca_dev *gspca_dev = video_drvdata(file);
  1035. int ret;
  1036. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1037. return -ERESTARTSYS;
  1038. ret = try_fmt_vid_cap(gspca_dev, fmt);
  1039. if (ret < 0)
  1040. goto out;
  1041. if (gspca_dev->nframes != 0
  1042. && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
  1043. ret = -EINVAL;
  1044. goto out;
  1045. }
  1046. if (ret == gspca_dev->curr_mode) {
  1047. ret = 0;
  1048. goto out; /* same mode */
  1049. }
  1050. if (gspca_dev->streaming) {
  1051. ret = -EBUSY;
  1052. goto out;
  1053. }
  1054. gspca_dev->width = fmt->fmt.pix.width;
  1055. gspca_dev->height = fmt->fmt.pix.height;
  1056. gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
  1057. gspca_dev->curr_mode = ret;
  1058. ret = 0;
  1059. out:
  1060. mutex_unlock(&gspca_dev->queue_lock);
  1061. return ret;
  1062. }
  1063. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1064. struct v4l2_frmsizeenum *fsize)
  1065. {
  1066. struct gspca_dev *gspca_dev = video_drvdata(file);
  1067. int i;
  1068. __u32 index = 0;
  1069. for (i = 0; i < gspca_dev->cam.nmodes; i++) {
  1070. if (fsize->pixel_format !=
  1071. gspca_dev->cam.cam_mode[i].pixelformat)
  1072. continue;
  1073. if (fsize->index == index) {
  1074. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1075. fsize->discrete.width =
  1076. gspca_dev->cam.cam_mode[i].width;
  1077. fsize->discrete.height =
  1078. gspca_dev->cam.cam_mode[i].height;
  1079. return 0;
  1080. }
  1081. index++;
  1082. }
  1083. return -EINVAL;
  1084. }
  1085. static int vidioc_enum_frameintervals(struct file *filp, void *priv,
  1086. struct v4l2_frmivalenum *fival)
  1087. {
  1088. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1089. int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
  1090. __u32 i;
  1091. if (gspca_dev->cam.mode_framerates == NULL ||
  1092. gspca_dev->cam.mode_framerates[mode].nrates == 0)
  1093. return -EINVAL;
  1094. if (fival->pixel_format !=
  1095. gspca_dev->cam.cam_mode[mode].pixelformat)
  1096. return -EINVAL;
  1097. for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
  1098. if (fival->index == i) {
  1099. fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1100. fival->discrete.numerator = 1;
  1101. fival->discrete.denominator =
  1102. gspca_dev->cam.mode_framerates[mode].rates[i];
  1103. return 0;
  1104. }
  1105. }
  1106. return -EINVAL;
  1107. }
  1108. static void gspca_release(struct v4l2_device *v4l2_device)
  1109. {
  1110. struct gspca_dev *gspca_dev =
  1111. container_of(v4l2_device, struct gspca_dev, v4l2_dev);
  1112. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1113. v4l2_device_unregister(&gspca_dev->v4l2_dev);
  1114. kfree(gspca_dev->usb_buf);
  1115. kfree(gspca_dev);
  1116. }
  1117. static int dev_open(struct file *file)
  1118. {
  1119. struct gspca_dev *gspca_dev = video_drvdata(file);
  1120. PDEBUG(D_STREAM, "[%s] open", current->comm);
  1121. /* protect the subdriver against rmmod */
  1122. if (!try_module_get(gspca_dev->module))
  1123. return -ENODEV;
  1124. return v4l2_fh_open(file);
  1125. }
  1126. static int dev_close(struct file *file)
  1127. {
  1128. struct gspca_dev *gspca_dev = video_drvdata(file);
  1129. PDEBUG(D_STREAM, "[%s] close", current->comm);
  1130. /* Needed for gspca_stream_off, always lock before queue_lock! */
  1131. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1132. return -ERESTARTSYS;
  1133. if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
  1134. mutex_unlock(&gspca_dev->usb_lock);
  1135. return -ERESTARTSYS;
  1136. }
  1137. /* if the file did the capture, free the streaming resources */
  1138. if (gspca_dev->capt_file == file) {
  1139. if (gspca_dev->streaming)
  1140. gspca_stream_off(gspca_dev);
  1141. frame_free(gspca_dev);
  1142. }
  1143. module_put(gspca_dev->module);
  1144. mutex_unlock(&gspca_dev->queue_lock);
  1145. mutex_unlock(&gspca_dev->usb_lock);
  1146. PDEBUG(D_STREAM, "close done");
  1147. return v4l2_fh_release(file);
  1148. }
  1149. static int vidioc_querycap(struct file *file, void *priv,
  1150. struct v4l2_capability *cap)
  1151. {
  1152. struct gspca_dev *gspca_dev = video_drvdata(file);
  1153. strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
  1154. sizeof cap->driver);
  1155. if (gspca_dev->dev->product != NULL) {
  1156. strlcpy((char *) cap->card, gspca_dev->dev->product,
  1157. sizeof cap->card);
  1158. } else {
  1159. snprintf((char *) cap->card, sizeof cap->card,
  1160. "USB Camera (%04x:%04x)",
  1161. le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
  1162. le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
  1163. }
  1164. usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
  1165. sizeof(cap->bus_info));
  1166. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  1167. | V4L2_CAP_STREAMING
  1168. | V4L2_CAP_READWRITE;
  1169. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1170. return 0;
  1171. }
  1172. static int vidioc_enum_input(struct file *file, void *priv,
  1173. struct v4l2_input *input)
  1174. {
  1175. struct gspca_dev *gspca_dev = video_drvdata(file);
  1176. if (input->index != 0)
  1177. return -EINVAL;
  1178. input->type = V4L2_INPUT_TYPE_CAMERA;
  1179. input->status = gspca_dev->cam.input_flags;
  1180. strlcpy(input->name, gspca_dev->sd_desc->name,
  1181. sizeof input->name);
  1182. return 0;
  1183. }
  1184. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1185. {
  1186. *i = 0;
  1187. return 0;
  1188. }
  1189. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1190. {
  1191. if (i > 0)
  1192. return -EINVAL;
  1193. return (0);
  1194. }
  1195. static int vidioc_reqbufs(struct file *file, void *priv,
  1196. struct v4l2_requestbuffers *rb)
  1197. {
  1198. struct gspca_dev *gspca_dev = video_drvdata(file);
  1199. int i, ret = 0, streaming;
  1200. i = rb->memory; /* (avoid compilation warning) */
  1201. switch (i) {
  1202. case GSPCA_MEMORY_READ: /* (internal call) */
  1203. case V4L2_MEMORY_MMAP:
  1204. case V4L2_MEMORY_USERPTR:
  1205. break;
  1206. default:
  1207. return -EINVAL;
  1208. }
  1209. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1210. return -ERESTARTSYS;
  1211. if (gspca_dev->memory != GSPCA_MEMORY_NO
  1212. && gspca_dev->memory != GSPCA_MEMORY_READ
  1213. && gspca_dev->memory != rb->memory) {
  1214. ret = -EBUSY;
  1215. goto out;
  1216. }
  1217. /* only one file may do the capture */
  1218. if (gspca_dev->capt_file != NULL
  1219. && gspca_dev->capt_file != file) {
  1220. ret = -EBUSY;
  1221. goto out;
  1222. }
  1223. /* if allocated, the buffers must not be mapped */
  1224. for (i = 0; i < gspca_dev->nframes; i++) {
  1225. if (gspca_dev->frame[i].vma_use_count) {
  1226. ret = -EBUSY;
  1227. goto out;
  1228. }
  1229. }
  1230. /* stop streaming */
  1231. streaming = gspca_dev->streaming;
  1232. if (streaming) {
  1233. gspca_stream_off(gspca_dev);
  1234. /* Don't restart the stream when switching from read
  1235. * to mmap mode */
  1236. if (gspca_dev->memory == GSPCA_MEMORY_READ)
  1237. streaming = 0;
  1238. }
  1239. /* free the previous allocated buffers, if any */
  1240. if (gspca_dev->nframes != 0)
  1241. frame_free(gspca_dev);
  1242. if (rb->count == 0) /* unrequest */
  1243. goto out;
  1244. ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
  1245. if (ret == 0) {
  1246. rb->count = gspca_dev->nframes;
  1247. if (streaming)
  1248. ret = gspca_init_transfer(gspca_dev);
  1249. }
  1250. out:
  1251. mutex_unlock(&gspca_dev->queue_lock);
  1252. PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
  1253. return ret;
  1254. }
  1255. static int vidioc_querybuf(struct file *file, void *priv,
  1256. struct v4l2_buffer *v4l2_buf)
  1257. {
  1258. struct gspca_dev *gspca_dev = video_drvdata(file);
  1259. struct gspca_frame *frame;
  1260. if (v4l2_buf->index >= gspca_dev->nframes)
  1261. return -EINVAL;
  1262. frame = &gspca_dev->frame[v4l2_buf->index];
  1263. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1264. return 0;
  1265. }
  1266. static int vidioc_streamon(struct file *file, void *priv,
  1267. enum v4l2_buf_type buf_type)
  1268. {
  1269. struct gspca_dev *gspca_dev = video_drvdata(file);
  1270. int ret;
  1271. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1272. return -EINVAL;
  1273. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1274. return -ERESTARTSYS;
  1275. /* check the capture file */
  1276. if (gspca_dev->capt_file != file) {
  1277. ret = -EBUSY;
  1278. goto out;
  1279. }
  1280. if (gspca_dev->nframes == 0
  1281. || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
  1282. ret = -EINVAL;
  1283. goto out;
  1284. }
  1285. if (!gspca_dev->streaming) {
  1286. ret = gspca_init_transfer(gspca_dev);
  1287. if (ret < 0)
  1288. goto out;
  1289. }
  1290. PDEBUG_MODE(gspca_dev, D_STREAM, "stream on OK", gspca_dev->pixfmt,
  1291. gspca_dev->width, gspca_dev->height);
  1292. ret = 0;
  1293. out:
  1294. mutex_unlock(&gspca_dev->queue_lock);
  1295. return ret;
  1296. }
  1297. static int vidioc_streamoff(struct file *file, void *priv,
  1298. enum v4l2_buf_type buf_type)
  1299. {
  1300. struct gspca_dev *gspca_dev = video_drvdata(file);
  1301. int i, ret;
  1302. if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1303. return -EINVAL;
  1304. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1305. return -ERESTARTSYS;
  1306. if (!gspca_dev->streaming) {
  1307. ret = 0;
  1308. goto out;
  1309. }
  1310. /* check the capture file */
  1311. if (gspca_dev->capt_file != file) {
  1312. ret = -EBUSY;
  1313. goto out;
  1314. }
  1315. /* stop streaming */
  1316. gspca_stream_off(gspca_dev);
  1317. /* In case another thread is waiting in dqbuf */
  1318. wake_up_interruptible(&gspca_dev->wq);
  1319. /* empty the transfer queues */
  1320. for (i = 0; i < gspca_dev->nframes; i++)
  1321. gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
  1322. atomic_set(&gspca_dev->fr_q, 0);
  1323. atomic_set(&gspca_dev->fr_i, 0);
  1324. gspca_dev->fr_o = 0;
  1325. ret = 0;
  1326. out:
  1327. mutex_unlock(&gspca_dev->queue_lock);
  1328. return ret;
  1329. }
  1330. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1331. struct v4l2_jpegcompression *jpegcomp)
  1332. {
  1333. struct gspca_dev *gspca_dev = video_drvdata(file);
  1334. gspca_dev->usb_err = 0;
  1335. return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
  1336. }
  1337. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1338. const struct v4l2_jpegcompression *jpegcomp)
  1339. {
  1340. struct gspca_dev *gspca_dev = video_drvdata(file);
  1341. gspca_dev->usb_err = 0;
  1342. return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
  1343. }
  1344. static int vidioc_g_parm(struct file *filp, void *priv,
  1345. struct v4l2_streamparm *parm)
  1346. {
  1347. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1348. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1349. if (gspca_dev->sd_desc->get_streamparm) {
  1350. gspca_dev->usb_err = 0;
  1351. gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
  1352. return gspca_dev->usb_err;
  1353. }
  1354. return 0;
  1355. }
  1356. static int vidioc_s_parm(struct file *filp, void *priv,
  1357. struct v4l2_streamparm *parm)
  1358. {
  1359. struct gspca_dev *gspca_dev = video_drvdata(filp);
  1360. int n;
  1361. n = parm->parm.capture.readbuffers;
  1362. if (n == 0 || n >= GSPCA_MAX_FRAMES)
  1363. parm->parm.capture.readbuffers = gspca_dev->nbufread;
  1364. else
  1365. gspca_dev->nbufread = n;
  1366. if (gspca_dev->sd_desc->set_streamparm) {
  1367. gspca_dev->usb_err = 0;
  1368. gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
  1369. return gspca_dev->usb_err;
  1370. }
  1371. return 0;
  1372. }
  1373. static int dev_mmap(struct file *file, struct vm_area_struct *vma)
  1374. {
  1375. struct gspca_dev *gspca_dev = video_drvdata(file);
  1376. struct gspca_frame *frame;
  1377. struct page *page;
  1378. unsigned long addr, start, size;
  1379. int i, ret;
  1380. start = vma->vm_start;
  1381. size = vma->vm_end - vma->vm_start;
  1382. PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
  1383. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1384. return -ERESTARTSYS;
  1385. if (gspca_dev->capt_file != file) {
  1386. ret = -EINVAL;
  1387. goto out;
  1388. }
  1389. frame = NULL;
  1390. for (i = 0; i < gspca_dev->nframes; ++i) {
  1391. if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
  1392. PDEBUG(D_STREAM, "mmap bad memory type");
  1393. break;
  1394. }
  1395. if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
  1396. == vma->vm_pgoff) {
  1397. frame = &gspca_dev->frame[i];
  1398. break;
  1399. }
  1400. }
  1401. if (frame == NULL) {
  1402. PDEBUG(D_STREAM, "mmap no frame buffer found");
  1403. ret = -EINVAL;
  1404. goto out;
  1405. }
  1406. if (size != frame->v4l2_buf.length) {
  1407. PDEBUG(D_STREAM, "mmap bad size");
  1408. ret = -EINVAL;
  1409. goto out;
  1410. }
  1411. /*
  1412. * - VM_IO marks the area as being a mmaped region for I/O to a
  1413. * device. It also prevents the region from being core dumped.
  1414. */
  1415. vma->vm_flags |= VM_IO;
  1416. addr = (unsigned long) frame->data;
  1417. while (size > 0) {
  1418. page = vmalloc_to_page((void *) addr);
  1419. ret = vm_insert_page(vma, start, page);
  1420. if (ret < 0)
  1421. goto out;
  1422. start += PAGE_SIZE;
  1423. addr += PAGE_SIZE;
  1424. size -= PAGE_SIZE;
  1425. }
  1426. vma->vm_ops = &gspca_vm_ops;
  1427. vma->vm_private_data = frame;
  1428. gspca_vm_open(vma);
  1429. ret = 0;
  1430. out:
  1431. mutex_unlock(&gspca_dev->queue_lock);
  1432. return ret;
  1433. }
  1434. static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
  1435. enum v4l2_memory memory)
  1436. {
  1437. if (!gspca_dev->present)
  1438. return -ENODEV;
  1439. if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
  1440. !gspca_dev->streaming)
  1441. return -EINVAL;
  1442. /* check if a frame is ready */
  1443. return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
  1444. }
  1445. static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
  1446. enum v4l2_memory memory)
  1447. {
  1448. int ret;
  1449. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1450. return -ERESTARTSYS;
  1451. ret = frame_ready_nolock(gspca_dev, file, memory);
  1452. mutex_unlock(&gspca_dev->queue_lock);
  1453. return ret;
  1454. }
  1455. /*
  1456. * dequeue a video buffer
  1457. *
  1458. * If nonblock_ing is false, block until a buffer is available.
  1459. */
  1460. static int vidioc_dqbuf(struct file *file, void *priv,
  1461. struct v4l2_buffer *v4l2_buf)
  1462. {
  1463. struct gspca_dev *gspca_dev = video_drvdata(file);
  1464. struct gspca_frame *frame;
  1465. int i, j, ret;
  1466. PDEBUG(D_FRAM, "dqbuf");
  1467. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1468. return -ERESTARTSYS;
  1469. for (;;) {
  1470. ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
  1471. if (ret < 0)
  1472. goto out;
  1473. if (ret > 0)
  1474. break;
  1475. mutex_unlock(&gspca_dev->queue_lock);
  1476. if (file->f_flags & O_NONBLOCK)
  1477. return -EAGAIN;
  1478. /* wait till a frame is ready */
  1479. ret = wait_event_interruptible_timeout(gspca_dev->wq,
  1480. frame_ready(gspca_dev, file, v4l2_buf->memory),
  1481. msecs_to_jiffies(3000));
  1482. if (ret < 0)
  1483. return ret;
  1484. if (ret == 0)
  1485. return -EIO;
  1486. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1487. return -ERESTARTSYS;
  1488. }
  1489. i = gspca_dev->fr_o;
  1490. j = gspca_dev->fr_queue[i];
  1491. frame = &gspca_dev->frame[j];
  1492. gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
  1493. frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
  1494. memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
  1495. PDEBUG(D_FRAM, "dqbuf %d", j);
  1496. ret = 0;
  1497. if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
  1498. if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
  1499. frame->data,
  1500. frame->v4l2_buf.bytesused)) {
  1501. PERR("dqbuf cp to user failed");
  1502. ret = -EFAULT;
  1503. }
  1504. }
  1505. out:
  1506. mutex_unlock(&gspca_dev->queue_lock);
  1507. if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
  1508. mutex_lock(&gspca_dev->usb_lock);
  1509. gspca_dev->usb_err = 0;
  1510. if (gspca_dev->present)
  1511. gspca_dev->sd_desc->dq_callback(gspca_dev);
  1512. mutex_unlock(&gspca_dev->usb_lock);
  1513. }
  1514. return ret;
  1515. }
  1516. /*
  1517. * queue a video buffer
  1518. *
  1519. * Attempting to queue a buffer that has already been
  1520. * queued will return -EINVAL.
  1521. */
  1522. static int vidioc_qbuf(struct file *file, void *priv,
  1523. struct v4l2_buffer *v4l2_buf)
  1524. {
  1525. struct gspca_dev *gspca_dev = video_drvdata(file);
  1526. struct gspca_frame *frame;
  1527. int i, index, ret;
  1528. PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
  1529. if (mutex_lock_interruptible(&gspca_dev->queue_lock))
  1530. return -ERESTARTSYS;
  1531. index = v4l2_buf->index;
  1532. if ((unsigned) index >= gspca_dev->nframes) {
  1533. PDEBUG(D_FRAM,
  1534. "qbuf idx %d >= %d", index, gspca_dev->nframes);
  1535. ret = -EINVAL;
  1536. goto out;
  1537. }
  1538. if (v4l2_buf->memory != gspca_dev->memory) {
  1539. PDEBUG(D_FRAM, "qbuf bad memory type");
  1540. ret = -EINVAL;
  1541. goto out;
  1542. }
  1543. frame = &gspca_dev->frame[index];
  1544. if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
  1545. PDEBUG(D_FRAM, "qbuf bad state");
  1546. ret = -EINVAL;
  1547. goto out;
  1548. }
  1549. frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
  1550. if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
  1551. frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
  1552. frame->v4l2_buf.length = v4l2_buf->length;
  1553. }
  1554. /* put the buffer in the 'queued' queue */
  1555. i = atomic_read(&gspca_dev->fr_q);
  1556. gspca_dev->fr_queue[i] = index;
  1557. atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
  1558. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1559. v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
  1560. ret = 0;
  1561. out:
  1562. mutex_unlock(&gspca_dev->queue_lock);
  1563. return ret;
  1564. }
  1565. /*
  1566. * allocate the resources for read()
  1567. */
  1568. static int read_alloc(struct gspca_dev *gspca_dev,
  1569. struct file *file)
  1570. {
  1571. struct v4l2_buffer v4l2_buf;
  1572. int i, ret;
  1573. PDEBUG(D_STREAM, "read alloc");
  1574. if (mutex_lock_interruptible(&gspca_dev->usb_lock))
  1575. return -ERESTARTSYS;
  1576. if (gspca_dev->nframes == 0) {
  1577. struct v4l2_requestbuffers rb;
  1578. memset(&rb, 0, sizeof rb);
  1579. rb.count = gspca_dev->nbufread;
  1580. rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1581. rb.memory = GSPCA_MEMORY_READ;
  1582. ret = vidioc_reqbufs(file, gspca_dev, &rb);
  1583. if (ret != 0) {
  1584. PDEBUG(D_STREAM, "read reqbuf err %d", ret);
  1585. goto out;
  1586. }
  1587. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1588. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1589. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1590. for (i = 0; i < gspca_dev->nbufread; i++) {
  1591. v4l2_buf.index = i;
  1592. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1593. if (ret != 0) {
  1594. PDEBUG(D_STREAM, "read qbuf err: %d", ret);
  1595. goto out;
  1596. }
  1597. }
  1598. }
  1599. /* start streaming */
  1600. ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1601. if (ret != 0)
  1602. PDEBUG(D_STREAM, "read streamon err %d", ret);
  1603. out:
  1604. mutex_unlock(&gspca_dev->usb_lock);
  1605. return ret;
  1606. }
  1607. static unsigned int dev_poll(struct file *file, poll_table *wait)
  1608. {
  1609. struct gspca_dev *gspca_dev = video_drvdata(file);
  1610. unsigned long req_events = poll_requested_events(wait);
  1611. int ret = 0;
  1612. PDEBUG(D_FRAM, "poll");
  1613. if (req_events & POLLPRI)
  1614. ret |= v4l2_ctrl_poll(file, wait);
  1615. if (req_events & (POLLIN | POLLRDNORM)) {
  1616. /* if reqbufs is not done, the user would use read() */
  1617. if (gspca_dev->memory == GSPCA_MEMORY_NO) {
  1618. if (read_alloc(gspca_dev, file) != 0) {
  1619. ret |= POLLERR;
  1620. goto out;
  1621. }
  1622. }
  1623. poll_wait(file, &gspca_dev->wq, wait);
  1624. /* check if an image has been received */
  1625. if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
  1626. ret |= POLLERR;
  1627. goto out;
  1628. }
  1629. if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
  1630. ret |= POLLIN | POLLRDNORM;
  1631. mutex_unlock(&gspca_dev->queue_lock);
  1632. }
  1633. out:
  1634. if (!gspca_dev->present)
  1635. ret |= POLLHUP;
  1636. return ret;
  1637. }
  1638. static ssize_t dev_read(struct file *file, char __user *data,
  1639. size_t count, loff_t *ppos)
  1640. {
  1641. struct gspca_dev *gspca_dev = video_drvdata(file);
  1642. struct gspca_frame *frame;
  1643. struct v4l2_buffer v4l2_buf;
  1644. struct timeval timestamp;
  1645. int n, ret, ret2;
  1646. PDEBUG(D_FRAM, "read (%zd)", count);
  1647. if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
  1648. ret = read_alloc(gspca_dev, file);
  1649. if (ret != 0)
  1650. return ret;
  1651. }
  1652. /* get a frame */
  1653. timestamp = ktime_to_timeval(ktime_get());
  1654. timestamp.tv_sec--;
  1655. n = 2;
  1656. for (;;) {
  1657. memset(&v4l2_buf, 0, sizeof v4l2_buf);
  1658. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1659. v4l2_buf.memory = GSPCA_MEMORY_READ;
  1660. ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
  1661. if (ret != 0) {
  1662. PDEBUG(D_STREAM, "read dqbuf err %d", ret);
  1663. return ret;
  1664. }
  1665. /* if the process slept for more than 1 second,
  1666. * get a newer frame */
  1667. frame = &gspca_dev->frame[v4l2_buf.index];
  1668. if (--n < 0)
  1669. break; /* avoid infinite loop */
  1670. if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
  1671. break;
  1672. ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1673. if (ret != 0) {
  1674. PDEBUG(D_STREAM, "read qbuf err %d", ret);
  1675. return ret;
  1676. }
  1677. }
  1678. /* copy the frame */
  1679. if (count > frame->v4l2_buf.bytesused)
  1680. count = frame->v4l2_buf.bytesused;
  1681. ret = copy_to_user(data, frame->data, count);
  1682. if (ret != 0) {
  1683. PERR("read cp to user lack %d / %zd", ret, count);
  1684. ret = -EFAULT;
  1685. goto out;
  1686. }
  1687. ret = count;
  1688. out:
  1689. /* in each case, requeue the buffer */
  1690. ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
  1691. if (ret2 != 0)
  1692. return ret2;
  1693. return ret;
  1694. }
  1695. static struct v4l2_file_operations dev_fops = {
  1696. .owner = THIS_MODULE,
  1697. .open = dev_open,
  1698. .release = dev_close,
  1699. .read = dev_read,
  1700. .mmap = dev_mmap,
  1701. .unlocked_ioctl = video_ioctl2,
  1702. .poll = dev_poll,
  1703. };
  1704. static const struct v4l2_ioctl_ops dev_ioctl_ops = {
  1705. .vidioc_querycap = vidioc_querycap,
  1706. .vidioc_dqbuf = vidioc_dqbuf,
  1707. .vidioc_qbuf = vidioc_qbuf,
  1708. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1709. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1710. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1711. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1712. .vidioc_streamon = vidioc_streamon,
  1713. .vidioc_enum_input = vidioc_enum_input,
  1714. .vidioc_g_input = vidioc_g_input,
  1715. .vidioc_s_input = vidioc_s_input,
  1716. .vidioc_reqbufs = vidioc_reqbufs,
  1717. .vidioc_querybuf = vidioc_querybuf,
  1718. .vidioc_streamoff = vidioc_streamoff,
  1719. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1720. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1721. .vidioc_g_parm = vidioc_g_parm,
  1722. .vidioc_s_parm = vidioc_s_parm,
  1723. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1724. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  1725. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1726. .vidioc_g_chip_info = vidioc_g_chip_info,
  1727. .vidioc_g_register = vidioc_g_register,
  1728. .vidioc_s_register = vidioc_s_register,
  1729. #endif
  1730. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1731. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1732. };
  1733. static const struct video_device gspca_template = {
  1734. .name = "gspca main driver",
  1735. .fops = &dev_fops,
  1736. .ioctl_ops = &dev_ioctl_ops,
  1737. .release = video_device_release_empty, /* We use v4l2_dev.release */
  1738. };
  1739. /*
  1740. * probe and create a new gspca device
  1741. *
  1742. * This function must be called by the sub-driver when it is
  1743. * called for probing a new device.
  1744. */
  1745. int gspca_dev_probe2(struct usb_interface *intf,
  1746. const struct usb_device_id *id,
  1747. const struct sd_desc *sd_desc,
  1748. int dev_size,
  1749. struct module *module)
  1750. {
  1751. struct gspca_dev *gspca_dev;
  1752. struct usb_device *dev = interface_to_usbdev(intf);
  1753. int ret;
  1754. pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
  1755. sd_desc->name, id->idVendor, id->idProduct);
  1756. /* create the device */
  1757. if (dev_size < sizeof *gspca_dev)
  1758. dev_size = sizeof *gspca_dev;
  1759. gspca_dev = kzalloc(dev_size, GFP_KERNEL);
  1760. if (!gspca_dev) {
  1761. pr_err("couldn't kzalloc gspca struct\n");
  1762. return -ENOMEM;
  1763. }
  1764. gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
  1765. if (!gspca_dev->usb_buf) {
  1766. pr_err("out of memory\n");
  1767. ret = -ENOMEM;
  1768. goto out;
  1769. }
  1770. gspca_dev->dev = dev;
  1771. gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
  1772. /* check if any audio device */
  1773. if (dev->actconfig->desc.bNumInterfaces != 1) {
  1774. int i;
  1775. struct usb_interface *intf2;
  1776. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1777. intf2 = dev->actconfig->interface[i];
  1778. if (intf2 != NULL
  1779. && intf2->altsetting != NULL
  1780. && intf2->altsetting->desc.bInterfaceClass ==
  1781. USB_CLASS_AUDIO) {
  1782. gspca_dev->audio = 1;
  1783. break;
  1784. }
  1785. }
  1786. }
  1787. gspca_dev->v4l2_dev.release = gspca_release;
  1788. ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
  1789. if (ret)
  1790. goto out;
  1791. gspca_dev->sd_desc = sd_desc;
  1792. gspca_dev->nbufread = 2;
  1793. gspca_dev->empty_packet = -1; /* don't check the empty packets */
  1794. gspca_dev->vdev = gspca_template;
  1795. gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
  1796. video_set_drvdata(&gspca_dev->vdev, gspca_dev);
  1797. set_bit(V4L2_FL_USE_FH_PRIO, &gspca_dev->vdev.flags);
  1798. gspca_dev->module = module;
  1799. gspca_dev->present = 1;
  1800. mutex_init(&gspca_dev->usb_lock);
  1801. gspca_dev->vdev.lock = &gspca_dev->usb_lock;
  1802. mutex_init(&gspca_dev->queue_lock);
  1803. init_waitqueue_head(&gspca_dev->wq);
  1804. /* configure the subdriver and initialize the USB device */
  1805. ret = sd_desc->config(gspca_dev, id);
  1806. if (ret < 0)
  1807. goto out;
  1808. ret = sd_desc->init(gspca_dev);
  1809. if (ret < 0)
  1810. goto out;
  1811. if (sd_desc->init_controls)
  1812. ret = sd_desc->init_controls(gspca_dev);
  1813. if (ret < 0)
  1814. goto out;
  1815. gspca_set_default_mode(gspca_dev);
  1816. ret = gspca_input_connect(gspca_dev);
  1817. if (ret)
  1818. goto out;
  1819. /*
  1820. * Don't take usb_lock for these ioctls. This improves latency if
  1821. * usb_lock is taken for a long time, e.g. when changing a control
  1822. * value, and a new frame is ready to be dequeued.
  1823. */
  1824. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
  1825. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
  1826. v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
  1827. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1828. if (!gspca_dev->sd_desc->get_register)
  1829. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
  1830. if (!gspca_dev->sd_desc->set_register)
  1831. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
  1832. #endif
  1833. if (!gspca_dev->sd_desc->get_jcomp)
  1834. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
  1835. if (!gspca_dev->sd_desc->set_jcomp)
  1836. v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
  1837. /* init video stuff */
  1838. ret = video_register_device(&gspca_dev->vdev,
  1839. VFL_TYPE_GRABBER,
  1840. -1);
  1841. if (ret < 0) {
  1842. pr_err("video_register_device err %d\n", ret);
  1843. goto out;
  1844. }
  1845. usb_set_intfdata(intf, gspca_dev);
  1846. PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
  1847. gspca_input_create_urb(gspca_dev);
  1848. return 0;
  1849. out:
  1850. #if IS_ENABLED(CONFIG_INPUT)
  1851. if (gspca_dev->input_dev)
  1852. input_unregister_device(gspca_dev->input_dev);
  1853. #endif
  1854. v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
  1855. kfree(gspca_dev->usb_buf);
  1856. kfree(gspca_dev);
  1857. return ret;
  1858. }
  1859. EXPORT_SYMBOL(gspca_dev_probe2);
  1860. /* same function as the previous one, but check the interface */
  1861. int gspca_dev_probe(struct usb_interface *intf,
  1862. const struct usb_device_id *id,
  1863. const struct sd_desc *sd_desc,
  1864. int dev_size,
  1865. struct module *module)
  1866. {
  1867. struct usb_device *dev = interface_to_usbdev(intf);
  1868. /* we don't handle multi-config cameras */
  1869. if (dev->descriptor.bNumConfigurations != 1) {
  1870. pr_err("%04x:%04x too many config\n",
  1871. id->idVendor, id->idProduct);
  1872. return -ENODEV;
  1873. }
  1874. /* the USB video interface must be the first one */
  1875. if (dev->actconfig->desc.bNumInterfaces != 1
  1876. && intf->cur_altsetting->desc.bInterfaceNumber != 0)
  1877. return -ENODEV;
  1878. return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
  1879. }
  1880. EXPORT_SYMBOL(gspca_dev_probe);
  1881. /*
  1882. * USB disconnection
  1883. *
  1884. * This function must be called by the sub-driver
  1885. * when the device disconnects, after the specific resources are freed.
  1886. */
  1887. void gspca_disconnect(struct usb_interface *intf)
  1888. {
  1889. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1890. #if IS_ENABLED(CONFIG_INPUT)
  1891. struct input_dev *input_dev;
  1892. #endif
  1893. PDEBUG(D_PROBE, "%s disconnect",
  1894. video_device_node_name(&gspca_dev->vdev));
  1895. mutex_lock(&gspca_dev->usb_lock);
  1896. gspca_dev->present = 0;
  1897. destroy_urbs(gspca_dev);
  1898. #if IS_ENABLED(CONFIG_INPUT)
  1899. gspca_input_destroy_urb(gspca_dev);
  1900. input_dev = gspca_dev->input_dev;
  1901. if (input_dev) {
  1902. gspca_dev->input_dev = NULL;
  1903. input_unregister_device(input_dev);
  1904. }
  1905. #endif
  1906. /* Free subdriver's streaming resources / stop sd workqueue(s) */
  1907. if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
  1908. gspca_dev->sd_desc->stop0(gspca_dev);
  1909. gspca_dev->streaming = 0;
  1910. gspca_dev->dev = NULL;
  1911. wake_up_interruptible(&gspca_dev->wq);
  1912. v4l2_device_disconnect(&gspca_dev->v4l2_dev);
  1913. video_unregister_device(&gspca_dev->vdev);
  1914. mutex_unlock(&gspca_dev->usb_lock);
  1915. /* (this will call gspca_release() immediately or on last close) */
  1916. v4l2_device_put(&gspca_dev->v4l2_dev);
  1917. }
  1918. EXPORT_SYMBOL(gspca_disconnect);
  1919. #ifdef CONFIG_PM
  1920. int gspca_suspend(struct usb_interface *intf, pm_message_t message)
  1921. {
  1922. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1923. gspca_input_destroy_urb(gspca_dev);
  1924. if (!gspca_dev->streaming)
  1925. return 0;
  1926. mutex_lock(&gspca_dev->usb_lock);
  1927. gspca_dev->frozen = 1; /* avoid urb error messages */
  1928. gspca_dev->usb_err = 0;
  1929. if (gspca_dev->sd_desc->stopN)
  1930. gspca_dev->sd_desc->stopN(gspca_dev);
  1931. destroy_urbs(gspca_dev);
  1932. gspca_set_alt0(gspca_dev);
  1933. if (gspca_dev->sd_desc->stop0)
  1934. gspca_dev->sd_desc->stop0(gspca_dev);
  1935. mutex_unlock(&gspca_dev->usb_lock);
  1936. return 0;
  1937. }
  1938. EXPORT_SYMBOL(gspca_suspend);
  1939. int gspca_resume(struct usb_interface *intf)
  1940. {
  1941. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  1942. int streaming, ret = 0;
  1943. mutex_lock(&gspca_dev->usb_lock);
  1944. gspca_dev->frozen = 0;
  1945. gspca_dev->usb_err = 0;
  1946. gspca_dev->sd_desc->init(gspca_dev);
  1947. /*
  1948. * Most subdrivers send all ctrl values on sd_start and thus
  1949. * only write to the device registers on s_ctrl when streaming ->
  1950. * Clear streaming to avoid setting all ctrls twice.
  1951. */
  1952. streaming = gspca_dev->streaming;
  1953. gspca_dev->streaming = 0;
  1954. if (streaming)
  1955. ret = gspca_init_transfer(gspca_dev);
  1956. else
  1957. gspca_input_create_urb(gspca_dev);
  1958. mutex_unlock(&gspca_dev->usb_lock);
  1959. return ret;
  1960. }
  1961. EXPORT_SYMBOL(gspca_resume);
  1962. #endif
  1963. /* -- module insert / remove -- */
  1964. static int __init gspca_init(void)
  1965. {
  1966. pr_info("v" GSPCA_VERSION " registered\n");
  1967. return 0;
  1968. }
  1969. static void __exit gspca_exit(void)
  1970. {
  1971. }
  1972. module_init(gspca_init);
  1973. module_exit(gspca_exit);
  1974. module_param_named(debug, gspca_debug, int, 0644);
  1975. MODULE_PARM_DESC(debug,
  1976. "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");