zr364xx.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  1. /*
  2. * Zoran 364xx based USB webcam module version 0.73
  3. *
  4. * Allows you to use your USB webcam with V4L2 applications
  5. * This is still in heavy developpement !
  6. *
  7. * Copyright (C) 2004 Antoine Jacquet <royale@zerezo.com>
  8. * http://royale.zerezo.com/zr364xx/
  9. *
  10. * Heavily inspired by usb-skeleton.c, vicam.c, cpia.c and spca50x.c drivers
  11. * V4L2 version inspired by meye.c driver
  12. *
  13. * Some video buffer code by Lamarque based on s2255drv.c and vivi.c drivers.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/usb.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/slab.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/highmem.h>
  36. #include <media/v4l2-common.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <media/v4l2-device.h>
  39. #include <media/v4l2-ctrls.h>
  40. #include <media/v4l2-fh.h>
  41. #include <media/v4l2-event.h>
  42. #include <media/videobuf-vmalloc.h>
  43. /* Version Information */
  44. #define DRIVER_VERSION "0.7.4"
  45. #define DRIVER_AUTHOR "Antoine Jacquet, http://royale.zerezo.com/"
  46. #define DRIVER_DESC "Zoran 364xx"
  47. /* Camera */
  48. #define FRAMES 1
  49. #define MAX_FRAME_SIZE 200000
  50. #define BUFFER_SIZE 0x1000
  51. #define CTRL_TIMEOUT 500
  52. #define ZR364XX_DEF_BUFS 4
  53. #define ZR364XX_READ_IDLE 0
  54. #define ZR364XX_READ_FRAME 1
  55. /* Debug macro */
  56. #define DBG(fmt, args...) \
  57. do { \
  58. if (debug) { \
  59. printk(KERN_INFO KBUILD_MODNAME " " fmt, ##args); \
  60. } \
  61. } while (0)
  62. /*#define FULL_DEBUG 1*/
  63. #ifdef FULL_DEBUG
  64. #define _DBG DBG
  65. #else
  66. #define _DBG(fmt, args...)
  67. #endif
  68. /* Init methods, need to find nicer names for these
  69. * the exact names of the chipsets would be the best if someone finds it */
  70. #define METHOD0 0
  71. #define METHOD1 1
  72. #define METHOD2 2
  73. #define METHOD3 3
  74. /* Module parameters */
  75. static int debug;
  76. static int mode;
  77. /* Module parameters interface */
  78. module_param(debug, int, 0644);
  79. MODULE_PARM_DESC(debug, "Debug level");
  80. module_param(mode, int, 0644);
  81. MODULE_PARM_DESC(mode, "0 = 320x240, 1 = 160x120, 2 = 640x480");
  82. /* Devices supported by this driver
  83. * .driver_info contains the init method used by the camera */
  84. static struct usb_device_id device_table[] = {
  85. {USB_DEVICE(0x08ca, 0x0109), .driver_info = METHOD0 },
  86. {USB_DEVICE(0x041e, 0x4024), .driver_info = METHOD0 },
  87. {USB_DEVICE(0x0d64, 0x0108), .driver_info = METHOD0 },
  88. {USB_DEVICE(0x0546, 0x3187), .driver_info = METHOD0 },
  89. {USB_DEVICE(0x0d64, 0x3108), .driver_info = METHOD0 },
  90. {USB_DEVICE(0x0595, 0x4343), .driver_info = METHOD0 },
  91. {USB_DEVICE(0x0bb0, 0x500d), .driver_info = METHOD0 },
  92. {USB_DEVICE(0x0feb, 0x2004), .driver_info = METHOD0 },
  93. {USB_DEVICE(0x055f, 0xb500), .driver_info = METHOD0 },
  94. {USB_DEVICE(0x08ca, 0x2062), .driver_info = METHOD2 },
  95. {USB_DEVICE(0x052b, 0x1a18), .driver_info = METHOD1 },
  96. {USB_DEVICE(0x04c8, 0x0729), .driver_info = METHOD0 },
  97. {USB_DEVICE(0x04f2, 0xa208), .driver_info = METHOD0 },
  98. {USB_DEVICE(0x0784, 0x0040), .driver_info = METHOD1 },
  99. {USB_DEVICE(0x06d6, 0x0034), .driver_info = METHOD0 },
  100. {USB_DEVICE(0x0a17, 0x0062), .driver_info = METHOD2 },
  101. {USB_DEVICE(0x06d6, 0x003b), .driver_info = METHOD0 },
  102. {USB_DEVICE(0x0a17, 0x004e), .driver_info = METHOD2 },
  103. {USB_DEVICE(0x041e, 0x405d), .driver_info = METHOD2 },
  104. {USB_DEVICE(0x08ca, 0x2102), .driver_info = METHOD3 },
  105. {USB_DEVICE(0x06d6, 0x003d), .driver_info = METHOD0 },
  106. {} /* Terminating entry */
  107. };
  108. MODULE_DEVICE_TABLE(usb, device_table);
  109. /* frame structure */
  110. struct zr364xx_framei {
  111. unsigned long ulState; /* ulState:ZR364XX_READ_IDLE,
  112. ZR364XX_READ_FRAME */
  113. void *lpvbits; /* image data */
  114. unsigned long cur_size; /* current data copied to it */
  115. };
  116. /* image buffer structure */
  117. struct zr364xx_bufferi {
  118. unsigned long dwFrames; /* number of frames in buffer */
  119. struct zr364xx_framei frame[FRAMES]; /* array of FRAME structures */
  120. };
  121. struct zr364xx_dmaqueue {
  122. struct list_head active;
  123. struct zr364xx_camera *cam;
  124. };
  125. struct zr364xx_pipeinfo {
  126. u32 transfer_size;
  127. u8 *transfer_buffer;
  128. u32 state;
  129. void *stream_urb;
  130. void *cam; /* back pointer to zr364xx_camera struct */
  131. u32 err_count;
  132. u32 idx;
  133. };
  134. struct zr364xx_fmt {
  135. char *name;
  136. u32 fourcc;
  137. int depth;
  138. };
  139. /* image formats. */
  140. static const struct zr364xx_fmt formats[] = {
  141. {
  142. .name = "JPG",
  143. .fourcc = V4L2_PIX_FMT_JPEG,
  144. .depth = 24
  145. }
  146. };
  147. /* Camera stuff */
  148. struct zr364xx_camera {
  149. struct usb_device *udev; /* save off the usb device pointer */
  150. struct usb_interface *interface;/* the interface for this device */
  151. struct v4l2_device v4l2_dev;
  152. struct v4l2_ctrl_handler ctrl_handler;
  153. struct video_device vdev; /* v4l video device */
  154. struct v4l2_fh *owner; /* owns the streaming */
  155. int nb;
  156. struct zr364xx_bufferi buffer;
  157. int skip;
  158. int width;
  159. int height;
  160. int method;
  161. struct mutex lock;
  162. spinlock_t slock;
  163. struct zr364xx_dmaqueue vidq;
  164. int last_frame;
  165. int cur_frame;
  166. unsigned long frame_count;
  167. int b_acquire;
  168. struct zr364xx_pipeinfo pipe[1];
  169. u8 read_endpoint;
  170. const struct zr364xx_fmt *fmt;
  171. struct videobuf_queue vb_vidq;
  172. };
  173. /* buffer for one video frame */
  174. struct zr364xx_buffer {
  175. /* common v4l buffer stuff -- must be first */
  176. struct videobuf_buffer vb;
  177. const struct zr364xx_fmt *fmt;
  178. };
  179. /* function used to send initialisation commands to the camera */
  180. static int send_control_msg(struct usb_device *udev, u8 request, u16 value,
  181. u16 index, unsigned char *cp, u16 size)
  182. {
  183. int status;
  184. unsigned char *transfer_buffer = kmalloc(size, GFP_KERNEL);
  185. if (!transfer_buffer) {
  186. dev_err(&udev->dev, "kmalloc(%d) failed\n", size);
  187. return -ENOMEM;
  188. }
  189. memcpy(transfer_buffer, cp, size);
  190. status = usb_control_msg(udev,
  191. usb_sndctrlpipe(udev, 0),
  192. request,
  193. USB_DIR_OUT | USB_TYPE_VENDOR |
  194. USB_RECIP_DEVICE, value, index,
  195. transfer_buffer, size, CTRL_TIMEOUT);
  196. kfree(transfer_buffer);
  197. return status;
  198. }
  199. /* Control messages sent to the camera to initialize it
  200. * and launch the capture */
  201. typedef struct {
  202. unsigned int value;
  203. unsigned int size;
  204. unsigned char *bytes;
  205. } message;
  206. /* method 0 */
  207. static unsigned char m0d1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  208. static unsigned char m0d2[] = { 0, 0, 0, 0, 0, 0 };
  209. static unsigned char m0d3[] = { 0, 0 };
  210. static message m0[] = {
  211. {0x1f30, 0, NULL},
  212. {0xd000, 0, NULL},
  213. {0x3370, sizeof(m0d1), m0d1},
  214. {0x2000, 0, NULL},
  215. {0x2f0f, 0, NULL},
  216. {0x2610, sizeof(m0d2), m0d2},
  217. {0xe107, 0, NULL},
  218. {0x2502, 0, NULL},
  219. {0x1f70, 0, NULL},
  220. {0xd000, 0, NULL},
  221. {0x9a01, sizeof(m0d3), m0d3},
  222. {-1, -1, NULL}
  223. };
  224. /* method 1 */
  225. static unsigned char m1d1[] = { 0xff, 0xff };
  226. static unsigned char m1d2[] = { 0x00, 0x00 };
  227. static message m1[] = {
  228. {0x1f30, 0, NULL},
  229. {0xd000, 0, NULL},
  230. {0xf000, 0, NULL},
  231. {0x2000, 0, NULL},
  232. {0x2f0f, 0, NULL},
  233. {0x2650, 0, NULL},
  234. {0xe107, 0, NULL},
  235. {0x2502, sizeof(m1d1), m1d1},
  236. {0x1f70, 0, NULL},
  237. {0xd000, 0, NULL},
  238. {0xd000, 0, NULL},
  239. {0xd000, 0, NULL},
  240. {0x9a01, sizeof(m1d2), m1d2},
  241. {-1, -1, NULL}
  242. };
  243. /* method 2 */
  244. static unsigned char m2d1[] = { 0xff, 0xff };
  245. static message m2[] = {
  246. {0x1f30, 0, NULL},
  247. {0xf000, 0, NULL},
  248. {0x2000, 0, NULL},
  249. {0x2f0f, 0, NULL},
  250. {0x2650, 0, NULL},
  251. {0xe107, 0, NULL},
  252. {0x2502, sizeof(m2d1), m2d1},
  253. {0x1f70, 0, NULL},
  254. {-1, -1, NULL}
  255. };
  256. /* init table */
  257. static message *init[4] = { m0, m1, m2, m2 };
  258. /* JPEG static data in header (Huffman table, etc) */
  259. static unsigned char header1[] = {
  260. 0xFF, 0xD8,
  261. /*
  262. 0xFF, 0xE0, 0x00, 0x10, 'J', 'F', 'I', 'F',
  263. 0x00, 0x01, 0x01, 0x00, 0x33, 0x8A, 0x00, 0x00, 0x33, 0x88,
  264. */
  265. 0xFF, 0xDB, 0x00, 0x84
  266. };
  267. static unsigned char header2[] = {
  268. 0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01,
  269. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  270. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  271. 0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02,
  272. 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01,
  273. 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
  274. 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
  275. 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33,
  276. 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25,
  277. 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  278. 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54,
  279. 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67,
  280. 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
  281. 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94,
  282. 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
  283. 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
  284. 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA,
  285. 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
  286. 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3,
  287. 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xC4, 0x00, 0x1F,
  288. 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  289. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  290. 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0xB5,
  291. 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05,
  292. 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
  293. 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  294. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1,
  295. 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16,
  296. 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27,
  297. 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
  298. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,
  299. 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
  300. 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84,
  301. 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
  302. 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
  303. 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
  304. 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3,
  305. 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5,
  306. 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  307. 0xF8, 0xF9, 0xFA, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0xF0, 0x01,
  308. 0x40, 0x03, 0x01, 0x21, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01,
  309. 0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11,
  310. 0x00, 0x3F, 0x00
  311. };
  312. static unsigned char header3;
  313. /* ------------------------------------------------------------------
  314. Videobuf operations
  315. ------------------------------------------------------------------*/
  316. static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
  317. unsigned int *size)
  318. {
  319. struct zr364xx_camera *cam = vq->priv_data;
  320. *size = cam->width * cam->height * (cam->fmt->depth >> 3);
  321. if (*count == 0)
  322. *count = ZR364XX_DEF_BUFS;
  323. if (*size * *count > ZR364XX_DEF_BUFS * 1024 * 1024)
  324. *count = (ZR364XX_DEF_BUFS * 1024 * 1024) / *size;
  325. return 0;
  326. }
  327. static void free_buffer(struct videobuf_queue *vq, struct zr364xx_buffer *buf)
  328. {
  329. _DBG("%s\n", __func__);
  330. if (in_interrupt())
  331. BUG();
  332. videobuf_vmalloc_free(&buf->vb);
  333. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  334. }
  335. static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  336. enum v4l2_field field)
  337. {
  338. struct zr364xx_camera *cam = vq->priv_data;
  339. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  340. vb);
  341. int rc;
  342. DBG("%s, field=%d, fmt name = %s\n", __func__, field, cam->fmt != NULL ?
  343. cam->fmt->name : "");
  344. if (cam->fmt == NULL)
  345. return -EINVAL;
  346. buf->vb.size = cam->width * cam->height * (cam->fmt->depth >> 3);
  347. if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size) {
  348. DBG("invalid buffer prepare\n");
  349. return -EINVAL;
  350. }
  351. buf->fmt = cam->fmt;
  352. buf->vb.width = cam->width;
  353. buf->vb.height = cam->height;
  354. buf->vb.field = field;
  355. if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
  356. rc = videobuf_iolock(vq, &buf->vb, NULL);
  357. if (rc < 0)
  358. goto fail;
  359. }
  360. buf->vb.state = VIDEOBUF_PREPARED;
  361. return 0;
  362. fail:
  363. free_buffer(vq, buf);
  364. return rc;
  365. }
  366. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  367. {
  368. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  369. vb);
  370. struct zr364xx_camera *cam = vq->priv_data;
  371. _DBG("%s\n", __func__);
  372. buf->vb.state = VIDEOBUF_QUEUED;
  373. list_add_tail(&buf->vb.queue, &cam->vidq.active);
  374. }
  375. static void buffer_release(struct videobuf_queue *vq,
  376. struct videobuf_buffer *vb)
  377. {
  378. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  379. vb);
  380. _DBG("%s\n", __func__);
  381. free_buffer(vq, buf);
  382. }
  383. static struct videobuf_queue_ops zr364xx_video_qops = {
  384. .buf_setup = buffer_setup,
  385. .buf_prepare = buffer_prepare,
  386. .buf_queue = buffer_queue,
  387. .buf_release = buffer_release,
  388. };
  389. /********************/
  390. /* V4L2 integration */
  391. /********************/
  392. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  393. enum v4l2_buf_type type);
  394. static ssize_t zr364xx_read(struct file *file, char __user *buf, size_t count,
  395. loff_t * ppos)
  396. {
  397. struct zr364xx_camera *cam = video_drvdata(file);
  398. int err = 0;
  399. _DBG("%s\n", __func__);
  400. if (!buf)
  401. return -EINVAL;
  402. if (!count)
  403. return -EINVAL;
  404. if (mutex_lock_interruptible(&cam->lock))
  405. return -ERESTARTSYS;
  406. err = zr364xx_vidioc_streamon(file, file->private_data,
  407. V4L2_BUF_TYPE_VIDEO_CAPTURE);
  408. if (err == 0) {
  409. DBG("%s: reading %d bytes at pos %d.\n", __func__,
  410. (int) count, (int) *ppos);
  411. /* NoMan Sux ! */
  412. err = videobuf_read_one(&cam->vb_vidq, buf, count, ppos,
  413. file->f_flags & O_NONBLOCK);
  414. }
  415. mutex_unlock(&cam->lock);
  416. return err;
  417. }
  418. /* video buffer vmalloc implementation based partly on VIVI driver which is
  419. * Copyright (c) 2006 by
  420. * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
  421. * Ted Walther <ted--a.t--enumera.com>
  422. * John Sokol <sokol--a.t--videotechnology.com>
  423. * http://v4l.videotechnology.com/
  424. *
  425. */
  426. static void zr364xx_fillbuff(struct zr364xx_camera *cam,
  427. struct zr364xx_buffer *buf,
  428. int jpgsize)
  429. {
  430. int pos = 0;
  431. struct timeval ts;
  432. const char *tmpbuf;
  433. char *vbuf = videobuf_to_vmalloc(&buf->vb);
  434. unsigned long last_frame;
  435. if (!vbuf)
  436. return;
  437. last_frame = cam->last_frame;
  438. if (last_frame != -1) {
  439. tmpbuf = (const char *)cam->buffer.frame[last_frame].lpvbits;
  440. switch (buf->fmt->fourcc) {
  441. case V4L2_PIX_FMT_JPEG:
  442. buf->vb.size = jpgsize;
  443. memcpy(vbuf, tmpbuf, buf->vb.size);
  444. break;
  445. default:
  446. printk(KERN_DEBUG KBUILD_MODNAME ": unknown format?\n");
  447. }
  448. cam->last_frame = -1;
  449. } else {
  450. printk(KERN_ERR KBUILD_MODNAME ": =======no frame\n");
  451. return;
  452. }
  453. DBG("%s: Buffer 0x%08lx size= %d\n", __func__,
  454. (unsigned long)vbuf, pos);
  455. /* tell v4l buffer was filled */
  456. buf->vb.field_count = cam->frame_count * 2;
  457. do_gettimeofday(&ts);
  458. buf->vb.ts = ts;
  459. buf->vb.state = VIDEOBUF_DONE;
  460. }
  461. static int zr364xx_got_frame(struct zr364xx_camera *cam, int jpgsize)
  462. {
  463. struct zr364xx_dmaqueue *dma_q = &cam->vidq;
  464. struct zr364xx_buffer *buf;
  465. unsigned long flags = 0;
  466. int rc = 0;
  467. DBG("wakeup: %p\n", &dma_q);
  468. spin_lock_irqsave(&cam->slock, flags);
  469. if (list_empty(&dma_q->active)) {
  470. DBG("No active queue to serve\n");
  471. rc = -1;
  472. goto unlock;
  473. }
  474. buf = list_entry(dma_q->active.next,
  475. struct zr364xx_buffer, vb.queue);
  476. if (!waitqueue_active(&buf->vb.done)) {
  477. /* no one active */
  478. rc = -1;
  479. goto unlock;
  480. }
  481. list_del(&buf->vb.queue);
  482. do_gettimeofday(&buf->vb.ts);
  483. DBG("[%p/%d] wakeup\n", buf, buf->vb.i);
  484. zr364xx_fillbuff(cam, buf, jpgsize);
  485. wake_up(&buf->vb.done);
  486. DBG("wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
  487. unlock:
  488. spin_unlock_irqrestore(&cam->slock, flags);
  489. return rc;
  490. }
  491. /* this function moves the usb stream read pipe data
  492. * into the system buffers.
  493. * returns 0 on success, EAGAIN if more data to process (call this
  494. * function again).
  495. */
  496. static int zr364xx_read_video_callback(struct zr364xx_camera *cam,
  497. struct zr364xx_pipeinfo *pipe_info,
  498. struct urb *purb)
  499. {
  500. unsigned char *pdest;
  501. unsigned char *psrc;
  502. s32 idx = -1;
  503. struct zr364xx_framei *frm;
  504. int i = 0;
  505. unsigned char *ptr = NULL;
  506. _DBG("buffer to user\n");
  507. idx = cam->cur_frame;
  508. frm = &cam->buffer.frame[idx];
  509. /* swap bytes if camera needs it */
  510. if (cam->method == METHOD0) {
  511. u16 *buf = (u16 *)pipe_info->transfer_buffer;
  512. for (i = 0; i < purb->actual_length/2; i++)
  513. swab16s(buf + i);
  514. }
  515. /* search done. now find out if should be acquiring */
  516. if (!cam->b_acquire) {
  517. /* we found a frame, but this channel is turned off */
  518. frm->ulState = ZR364XX_READ_IDLE;
  519. return -EINVAL;
  520. }
  521. psrc = (u8 *)pipe_info->transfer_buffer;
  522. ptr = pdest = frm->lpvbits;
  523. if (frm->ulState == ZR364XX_READ_IDLE) {
  524. frm->ulState = ZR364XX_READ_FRAME;
  525. frm->cur_size = 0;
  526. _DBG("jpeg header, ");
  527. memcpy(ptr, header1, sizeof(header1));
  528. ptr += sizeof(header1);
  529. header3 = 0;
  530. memcpy(ptr, &header3, 1);
  531. ptr++;
  532. memcpy(ptr, psrc, 64);
  533. ptr += 64;
  534. header3 = 1;
  535. memcpy(ptr, &header3, 1);
  536. ptr++;
  537. memcpy(ptr, psrc + 64, 64);
  538. ptr += 64;
  539. memcpy(ptr, header2, sizeof(header2));
  540. ptr += sizeof(header2);
  541. memcpy(ptr, psrc + 128,
  542. purb->actual_length - 128);
  543. ptr += purb->actual_length - 128;
  544. _DBG("header : %d %d %d %d %d %d %d %d %d\n",
  545. psrc[0], psrc[1], psrc[2],
  546. psrc[3], psrc[4], psrc[5],
  547. psrc[6], psrc[7], psrc[8]);
  548. frm->cur_size = ptr - pdest;
  549. } else {
  550. if (frm->cur_size + purb->actual_length > MAX_FRAME_SIZE) {
  551. dev_info(&cam->udev->dev,
  552. "%s: buffer (%d bytes) too small to hold "
  553. "frame data. Discarding frame data.\n",
  554. __func__, MAX_FRAME_SIZE);
  555. } else {
  556. pdest += frm->cur_size;
  557. memcpy(pdest, psrc, purb->actual_length);
  558. frm->cur_size += purb->actual_length;
  559. }
  560. }
  561. /*_DBG("cur_size %lu urb size %d\n", frm->cur_size,
  562. purb->actual_length);*/
  563. if (purb->actual_length < pipe_info->transfer_size) {
  564. _DBG("****************Buffer[%d]full*************\n", idx);
  565. cam->last_frame = cam->cur_frame;
  566. cam->cur_frame++;
  567. /* end of system frame ring buffer, start at zero */
  568. if (cam->cur_frame == cam->buffer.dwFrames)
  569. cam->cur_frame = 0;
  570. /* frame ready */
  571. /* go back to find the JPEG EOI marker */
  572. ptr = pdest = frm->lpvbits;
  573. ptr += frm->cur_size - 2;
  574. while (ptr > pdest) {
  575. if (*ptr == 0xFF && *(ptr + 1) == 0xD9
  576. && *(ptr + 2) == 0xFF)
  577. break;
  578. ptr--;
  579. }
  580. if (ptr == pdest)
  581. DBG("No EOI marker\n");
  582. /* Sometimes there is junk data in the middle of the picture,
  583. * we want to skip this bogus frames */
  584. while (ptr > pdest) {
  585. if (*ptr == 0xFF && *(ptr + 1) == 0xFF
  586. && *(ptr + 2) == 0xFF)
  587. break;
  588. ptr--;
  589. }
  590. if (ptr != pdest) {
  591. DBG("Bogus frame ? %d\n", ++(cam->nb));
  592. } else if (cam->b_acquire) {
  593. /* we skip the 2 first frames which are usually buggy */
  594. if (cam->skip)
  595. cam->skip--;
  596. else {
  597. _DBG("jpeg(%lu): %d %d %d %d %d %d %d %d\n",
  598. frm->cur_size,
  599. pdest[0], pdest[1], pdest[2], pdest[3],
  600. pdest[4], pdest[5], pdest[6], pdest[7]);
  601. zr364xx_got_frame(cam, frm->cur_size);
  602. }
  603. }
  604. cam->frame_count++;
  605. frm->ulState = ZR364XX_READ_IDLE;
  606. frm->cur_size = 0;
  607. }
  608. /* done successfully */
  609. return 0;
  610. }
  611. static int zr364xx_vidioc_querycap(struct file *file, void *priv,
  612. struct v4l2_capability *cap)
  613. {
  614. struct zr364xx_camera *cam = video_drvdata(file);
  615. strlcpy(cap->driver, DRIVER_DESC, sizeof(cap->driver));
  616. strlcpy(cap->card, cam->udev->product, sizeof(cap->card));
  617. strlcpy(cap->bus_info, dev_name(&cam->udev->dev),
  618. sizeof(cap->bus_info));
  619. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
  620. V4L2_CAP_READWRITE |
  621. V4L2_CAP_STREAMING;
  622. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  623. return 0;
  624. }
  625. static int zr364xx_vidioc_enum_input(struct file *file, void *priv,
  626. struct v4l2_input *i)
  627. {
  628. if (i->index != 0)
  629. return -EINVAL;
  630. strcpy(i->name, DRIVER_DESC " Camera");
  631. i->type = V4L2_INPUT_TYPE_CAMERA;
  632. return 0;
  633. }
  634. static int zr364xx_vidioc_g_input(struct file *file, void *priv,
  635. unsigned int *i)
  636. {
  637. *i = 0;
  638. return 0;
  639. }
  640. static int zr364xx_vidioc_s_input(struct file *file, void *priv,
  641. unsigned int i)
  642. {
  643. if (i != 0)
  644. return -EINVAL;
  645. return 0;
  646. }
  647. static int zr364xx_s_ctrl(struct v4l2_ctrl *ctrl)
  648. {
  649. struct zr364xx_camera *cam =
  650. container_of(ctrl->handler, struct zr364xx_camera, ctrl_handler);
  651. int temp;
  652. switch (ctrl->id) {
  653. case V4L2_CID_BRIGHTNESS:
  654. /* hardware brightness */
  655. send_control_msg(cam->udev, 1, 0x2001, 0, NULL, 0);
  656. temp = (0x60 << 8) + 127 - ctrl->val;
  657. send_control_msg(cam->udev, 1, temp, 0, NULL, 0);
  658. break;
  659. default:
  660. return -EINVAL;
  661. }
  662. return 0;
  663. }
  664. static int zr364xx_vidioc_enum_fmt_vid_cap(struct file *file,
  665. void *priv, struct v4l2_fmtdesc *f)
  666. {
  667. if (f->index > 0)
  668. return -EINVAL;
  669. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  670. strcpy(f->description, formats[0].name);
  671. f->pixelformat = formats[0].fourcc;
  672. return 0;
  673. }
  674. static char *decode_fourcc(__u32 pixelformat, char *buf)
  675. {
  676. buf[0] = pixelformat & 0xff;
  677. buf[1] = (pixelformat >> 8) & 0xff;
  678. buf[2] = (pixelformat >> 16) & 0xff;
  679. buf[3] = (pixelformat >> 24) & 0xff;
  680. buf[4] = '\0';
  681. return buf;
  682. }
  683. static int zr364xx_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  684. struct v4l2_format *f)
  685. {
  686. struct zr364xx_camera *cam = video_drvdata(file);
  687. char pixelformat_name[5];
  688. if (cam == NULL)
  689. return -ENODEV;
  690. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG) {
  691. DBG("%s: unsupported pixelformat V4L2_PIX_FMT_%s\n", __func__,
  692. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name));
  693. return -EINVAL;
  694. }
  695. if (!(f->fmt.pix.width == 160 && f->fmt.pix.height == 120) &&
  696. !(f->fmt.pix.width == 640 && f->fmt.pix.height == 480)) {
  697. f->fmt.pix.width = 320;
  698. f->fmt.pix.height = 240;
  699. }
  700. f->fmt.pix.field = V4L2_FIELD_NONE;
  701. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  702. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  703. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  704. f->fmt.pix.priv = 0;
  705. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  706. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  707. f->fmt.pix.field);
  708. return 0;
  709. }
  710. static int zr364xx_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  711. struct v4l2_format *f)
  712. {
  713. struct zr364xx_camera *cam;
  714. if (file == NULL)
  715. return -ENODEV;
  716. cam = video_drvdata(file);
  717. f->fmt.pix.pixelformat = formats[0].fourcc;
  718. f->fmt.pix.field = V4L2_FIELD_NONE;
  719. f->fmt.pix.width = cam->width;
  720. f->fmt.pix.height = cam->height;
  721. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  722. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  723. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  724. f->fmt.pix.priv = 0;
  725. return 0;
  726. }
  727. static int zr364xx_vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  728. struct v4l2_format *f)
  729. {
  730. struct zr364xx_camera *cam = video_drvdata(file);
  731. struct videobuf_queue *q = &cam->vb_vidq;
  732. char pixelformat_name[5];
  733. int ret = zr364xx_vidioc_try_fmt_vid_cap(file, cam, f);
  734. int i;
  735. if (ret < 0)
  736. return ret;
  737. mutex_lock(&q->vb_lock);
  738. if (videobuf_queue_is_busy(&cam->vb_vidq)) {
  739. DBG("%s queue busy\n", __func__);
  740. ret = -EBUSY;
  741. goto out;
  742. }
  743. if (cam->owner) {
  744. DBG("%s can't change format after started\n", __func__);
  745. ret = -EBUSY;
  746. goto out;
  747. }
  748. cam->width = f->fmt.pix.width;
  749. cam->height = f->fmt.pix.height;
  750. DBG("%s: %dx%d mode selected\n", __func__,
  751. cam->width, cam->height);
  752. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  753. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  754. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  755. f->fmt.pix.priv = 0;
  756. cam->vb_vidq.field = f->fmt.pix.field;
  757. if (f->fmt.pix.width == 160 && f->fmt.pix.height == 120)
  758. mode = 1;
  759. else if (f->fmt.pix.width == 640 && f->fmt.pix.height == 480)
  760. mode = 2;
  761. else
  762. mode = 0;
  763. m0d1[0] = mode;
  764. m1[2].value = 0xf000 + mode;
  765. m2[1].value = 0xf000 + mode;
  766. /* special case for METHOD3, the modes are different */
  767. if (cam->method == METHOD3) {
  768. switch (mode) {
  769. case 1:
  770. m2[1].value = 0xf000 + 4;
  771. break;
  772. case 2:
  773. m2[1].value = 0xf000 + 0;
  774. break;
  775. default:
  776. m2[1].value = 0xf000 + 1;
  777. break;
  778. }
  779. }
  780. header2[437] = cam->height / 256;
  781. header2[438] = cam->height % 256;
  782. header2[439] = cam->width / 256;
  783. header2[440] = cam->width % 256;
  784. for (i = 0; init[cam->method][i].size != -1; i++) {
  785. ret =
  786. send_control_msg(cam->udev, 1, init[cam->method][i].value,
  787. 0, init[cam->method][i].bytes,
  788. init[cam->method][i].size);
  789. if (ret < 0) {
  790. dev_err(&cam->udev->dev,
  791. "error during resolution change sequence: %d\n", i);
  792. goto out;
  793. }
  794. }
  795. /* Added some delay here, since opening/closing the camera quickly,
  796. * like Ekiga does during its startup, can crash the webcam
  797. */
  798. mdelay(100);
  799. cam->skip = 2;
  800. ret = 0;
  801. out:
  802. mutex_unlock(&q->vb_lock);
  803. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  804. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  805. f->fmt.pix.field);
  806. return ret;
  807. }
  808. static int zr364xx_vidioc_reqbufs(struct file *file, void *priv,
  809. struct v4l2_requestbuffers *p)
  810. {
  811. struct zr364xx_camera *cam = video_drvdata(file);
  812. if (cam->owner && cam->owner != priv)
  813. return -EBUSY;
  814. return videobuf_reqbufs(&cam->vb_vidq, p);
  815. }
  816. static int zr364xx_vidioc_querybuf(struct file *file,
  817. void *priv,
  818. struct v4l2_buffer *p)
  819. {
  820. int rc;
  821. struct zr364xx_camera *cam = video_drvdata(file);
  822. rc = videobuf_querybuf(&cam->vb_vidq, p);
  823. return rc;
  824. }
  825. static int zr364xx_vidioc_qbuf(struct file *file,
  826. void *priv,
  827. struct v4l2_buffer *p)
  828. {
  829. int rc;
  830. struct zr364xx_camera *cam = video_drvdata(file);
  831. _DBG("%s\n", __func__);
  832. if (cam->owner && cam->owner != priv)
  833. return -EBUSY;
  834. rc = videobuf_qbuf(&cam->vb_vidq, p);
  835. return rc;
  836. }
  837. static int zr364xx_vidioc_dqbuf(struct file *file,
  838. void *priv,
  839. struct v4l2_buffer *p)
  840. {
  841. int rc;
  842. struct zr364xx_camera *cam = video_drvdata(file);
  843. _DBG("%s\n", __func__);
  844. if (cam->owner && cam->owner != priv)
  845. return -EBUSY;
  846. rc = videobuf_dqbuf(&cam->vb_vidq, p, file->f_flags & O_NONBLOCK);
  847. return rc;
  848. }
  849. static void read_pipe_completion(struct urb *purb)
  850. {
  851. struct zr364xx_pipeinfo *pipe_info;
  852. struct zr364xx_camera *cam;
  853. int pipe;
  854. pipe_info = purb->context;
  855. _DBG("%s %p, status %d\n", __func__, purb, purb->status);
  856. if (pipe_info == NULL) {
  857. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  858. return;
  859. }
  860. cam = pipe_info->cam;
  861. if (cam == NULL) {
  862. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  863. return;
  864. }
  865. /* if shutting down, do not resubmit, exit immediately */
  866. if (purb->status == -ESHUTDOWN) {
  867. DBG("%s, err shutdown\n", __func__);
  868. pipe_info->err_count++;
  869. return;
  870. }
  871. if (pipe_info->state == 0) {
  872. DBG("exiting USB pipe\n");
  873. return;
  874. }
  875. if (purb->actual_length < 0 ||
  876. purb->actual_length > pipe_info->transfer_size) {
  877. dev_err(&cam->udev->dev, "wrong number of bytes\n");
  878. return;
  879. }
  880. if (purb->status == 0)
  881. zr364xx_read_video_callback(cam, pipe_info, purb);
  882. else {
  883. pipe_info->err_count++;
  884. DBG("%s: failed URB %d\n", __func__, purb->status);
  885. }
  886. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  887. /* reuse urb */
  888. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  889. pipe,
  890. pipe_info->transfer_buffer,
  891. pipe_info->transfer_size,
  892. read_pipe_completion, pipe_info);
  893. if (pipe_info->state != 0) {
  894. purb->status = usb_submit_urb(pipe_info->stream_urb,
  895. GFP_ATOMIC);
  896. if (purb->status)
  897. dev_err(&cam->udev->dev,
  898. "error submitting urb (error=%i)\n",
  899. purb->status);
  900. } else
  901. DBG("read pipe complete state 0\n");
  902. }
  903. static int zr364xx_start_readpipe(struct zr364xx_camera *cam)
  904. {
  905. int pipe;
  906. int retval;
  907. struct zr364xx_pipeinfo *pipe_info = cam->pipe;
  908. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  909. DBG("%s: start pipe IN x%x\n", __func__, cam->read_endpoint);
  910. pipe_info->state = 1;
  911. pipe_info->err_count = 0;
  912. pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
  913. if (!pipe_info->stream_urb) {
  914. dev_err(&cam->udev->dev, "ReadStream: Unable to alloc URB\n");
  915. return -ENOMEM;
  916. }
  917. /* transfer buffer allocated in board_init */
  918. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  919. pipe,
  920. pipe_info->transfer_buffer,
  921. pipe_info->transfer_size,
  922. read_pipe_completion, pipe_info);
  923. DBG("submitting URB %p\n", pipe_info->stream_urb);
  924. retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
  925. if (retval) {
  926. printk(KERN_ERR KBUILD_MODNAME ": start read pipe failed\n");
  927. return retval;
  928. }
  929. return 0;
  930. }
  931. static void zr364xx_stop_readpipe(struct zr364xx_camera *cam)
  932. {
  933. struct zr364xx_pipeinfo *pipe_info;
  934. if (cam == NULL) {
  935. printk(KERN_ERR KBUILD_MODNAME ": invalid device\n");
  936. return;
  937. }
  938. DBG("stop read pipe\n");
  939. pipe_info = cam->pipe;
  940. if (pipe_info) {
  941. if (pipe_info->state != 0)
  942. pipe_info->state = 0;
  943. if (pipe_info->stream_urb) {
  944. /* cancel urb */
  945. usb_kill_urb(pipe_info->stream_urb);
  946. usb_free_urb(pipe_info->stream_urb);
  947. pipe_info->stream_urb = NULL;
  948. }
  949. }
  950. return;
  951. }
  952. /* starts acquisition process */
  953. static int zr364xx_start_acquire(struct zr364xx_camera *cam)
  954. {
  955. int j;
  956. DBG("start acquire\n");
  957. cam->last_frame = -1;
  958. cam->cur_frame = 0;
  959. for (j = 0; j < FRAMES; j++) {
  960. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  961. cam->buffer.frame[j].cur_size = 0;
  962. }
  963. cam->b_acquire = 1;
  964. return 0;
  965. }
  966. static inline int zr364xx_stop_acquire(struct zr364xx_camera *cam)
  967. {
  968. cam->b_acquire = 0;
  969. return 0;
  970. }
  971. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  972. enum v4l2_buf_type type)
  973. {
  974. struct zr364xx_camera *cam = video_drvdata(file);
  975. int i, j;
  976. int res;
  977. DBG("%s\n", __func__);
  978. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  979. return -EINVAL;
  980. if (cam->owner && cam->owner != priv)
  981. return -EBUSY;
  982. for (i = 0; init[cam->method][i].size != -1; i++) {
  983. res = send_control_msg(cam->udev, 1, init[cam->method][i].value,
  984. 0, init[cam->method][i].bytes,
  985. init[cam->method][i].size);
  986. if (res < 0) {
  987. dev_err(&cam->udev->dev,
  988. "error during open sequence: %d\n", i);
  989. return res;
  990. }
  991. }
  992. cam->skip = 2;
  993. cam->last_frame = -1;
  994. cam->cur_frame = 0;
  995. cam->frame_count = 0;
  996. for (j = 0; j < FRAMES; j++) {
  997. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  998. cam->buffer.frame[j].cur_size = 0;
  999. }
  1000. res = videobuf_streamon(&cam->vb_vidq);
  1001. if (res == 0) {
  1002. zr364xx_start_acquire(cam);
  1003. cam->owner = file->private_data;
  1004. }
  1005. return res;
  1006. }
  1007. static int zr364xx_vidioc_streamoff(struct file *file, void *priv,
  1008. enum v4l2_buf_type type)
  1009. {
  1010. struct zr364xx_camera *cam = video_drvdata(file);
  1011. DBG("%s\n", __func__);
  1012. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1013. return -EINVAL;
  1014. if (cam->owner && cam->owner != priv)
  1015. return -EBUSY;
  1016. zr364xx_stop_acquire(cam);
  1017. return videobuf_streamoff(&cam->vb_vidq);
  1018. }
  1019. /* open the camera */
  1020. static int zr364xx_open(struct file *file)
  1021. {
  1022. struct zr364xx_camera *cam = video_drvdata(file);
  1023. int err;
  1024. DBG("%s\n", __func__);
  1025. if (mutex_lock_interruptible(&cam->lock))
  1026. return -ERESTARTSYS;
  1027. err = v4l2_fh_open(file);
  1028. if (err)
  1029. goto out;
  1030. /* Added some delay here, since opening/closing the camera quickly,
  1031. * like Ekiga does during its startup, can crash the webcam
  1032. */
  1033. mdelay(100);
  1034. err = 0;
  1035. out:
  1036. mutex_unlock(&cam->lock);
  1037. DBG("%s: %d\n", __func__, err);
  1038. return err;
  1039. }
  1040. static void zr364xx_release(struct v4l2_device *v4l2_dev)
  1041. {
  1042. struct zr364xx_camera *cam =
  1043. container_of(v4l2_dev, struct zr364xx_camera, v4l2_dev);
  1044. unsigned long i;
  1045. v4l2_device_unregister(&cam->v4l2_dev);
  1046. videobuf_mmap_free(&cam->vb_vidq);
  1047. /* release sys buffers */
  1048. for (i = 0; i < FRAMES; i++) {
  1049. if (cam->buffer.frame[i].lpvbits) {
  1050. DBG("vfree %p\n", cam->buffer.frame[i].lpvbits);
  1051. vfree(cam->buffer.frame[i].lpvbits);
  1052. }
  1053. cam->buffer.frame[i].lpvbits = NULL;
  1054. }
  1055. v4l2_ctrl_handler_free(&cam->ctrl_handler);
  1056. /* release transfer buffer */
  1057. kfree(cam->pipe->transfer_buffer);
  1058. kfree(cam);
  1059. }
  1060. /* release the camera */
  1061. static int zr364xx_close(struct file *file)
  1062. {
  1063. struct zr364xx_camera *cam;
  1064. struct usb_device *udev;
  1065. int i;
  1066. DBG("%s\n", __func__);
  1067. cam = video_drvdata(file);
  1068. mutex_lock(&cam->lock);
  1069. udev = cam->udev;
  1070. if (file->private_data == cam->owner) {
  1071. /* turn off stream */
  1072. if (cam->b_acquire)
  1073. zr364xx_stop_acquire(cam);
  1074. videobuf_streamoff(&cam->vb_vidq);
  1075. for (i = 0; i < 2; i++) {
  1076. send_control_msg(udev, 1, init[cam->method][i].value,
  1077. 0, init[cam->method][i].bytes,
  1078. init[cam->method][i].size);
  1079. }
  1080. cam->owner = NULL;
  1081. }
  1082. /* Added some delay here, since opening/closing the camera quickly,
  1083. * like Ekiga does during its startup, can crash the webcam
  1084. */
  1085. mdelay(100);
  1086. mutex_unlock(&cam->lock);
  1087. return v4l2_fh_release(file);
  1088. }
  1089. static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
  1090. {
  1091. struct zr364xx_camera *cam = video_drvdata(file);
  1092. int ret;
  1093. if (cam == NULL) {
  1094. DBG("%s: cam == NULL\n", __func__);
  1095. return -ENODEV;
  1096. }
  1097. DBG("mmap called, vma=0x%08lx\n", (unsigned long)vma);
  1098. ret = videobuf_mmap_mapper(&cam->vb_vidq, vma);
  1099. DBG("vma start=0x%08lx, size=%ld, ret=%d\n",
  1100. (unsigned long)vma->vm_start,
  1101. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
  1102. return ret;
  1103. }
  1104. static unsigned int zr364xx_poll(struct file *file,
  1105. struct poll_table_struct *wait)
  1106. {
  1107. struct zr364xx_camera *cam = video_drvdata(file);
  1108. struct videobuf_queue *q = &cam->vb_vidq;
  1109. unsigned res = v4l2_ctrl_poll(file, wait);
  1110. _DBG("%s\n", __func__);
  1111. return res | videobuf_poll_stream(file, q, wait);
  1112. }
  1113. static const struct v4l2_ctrl_ops zr364xx_ctrl_ops = {
  1114. .s_ctrl = zr364xx_s_ctrl,
  1115. };
  1116. static const struct v4l2_file_operations zr364xx_fops = {
  1117. .owner = THIS_MODULE,
  1118. .open = zr364xx_open,
  1119. .release = zr364xx_close,
  1120. .read = zr364xx_read,
  1121. .mmap = zr364xx_mmap,
  1122. .unlocked_ioctl = video_ioctl2,
  1123. .poll = zr364xx_poll,
  1124. };
  1125. static const struct v4l2_ioctl_ops zr364xx_ioctl_ops = {
  1126. .vidioc_querycap = zr364xx_vidioc_querycap,
  1127. .vidioc_enum_fmt_vid_cap = zr364xx_vidioc_enum_fmt_vid_cap,
  1128. .vidioc_try_fmt_vid_cap = zr364xx_vidioc_try_fmt_vid_cap,
  1129. .vidioc_s_fmt_vid_cap = zr364xx_vidioc_s_fmt_vid_cap,
  1130. .vidioc_g_fmt_vid_cap = zr364xx_vidioc_g_fmt_vid_cap,
  1131. .vidioc_enum_input = zr364xx_vidioc_enum_input,
  1132. .vidioc_g_input = zr364xx_vidioc_g_input,
  1133. .vidioc_s_input = zr364xx_vidioc_s_input,
  1134. .vidioc_streamon = zr364xx_vidioc_streamon,
  1135. .vidioc_streamoff = zr364xx_vidioc_streamoff,
  1136. .vidioc_reqbufs = zr364xx_vidioc_reqbufs,
  1137. .vidioc_querybuf = zr364xx_vidioc_querybuf,
  1138. .vidioc_qbuf = zr364xx_vidioc_qbuf,
  1139. .vidioc_dqbuf = zr364xx_vidioc_dqbuf,
  1140. .vidioc_log_status = v4l2_ctrl_log_status,
  1141. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1142. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1143. };
  1144. static struct video_device zr364xx_template = {
  1145. .name = DRIVER_DESC,
  1146. .fops = &zr364xx_fops,
  1147. .ioctl_ops = &zr364xx_ioctl_ops,
  1148. .release = video_device_release_empty,
  1149. };
  1150. /*******************/
  1151. /* USB integration */
  1152. /*******************/
  1153. static int zr364xx_board_init(struct zr364xx_camera *cam)
  1154. {
  1155. struct zr364xx_pipeinfo *pipe = cam->pipe;
  1156. unsigned long i;
  1157. DBG("board init: %p\n", cam);
  1158. memset(pipe, 0, sizeof(*pipe));
  1159. pipe->cam = cam;
  1160. pipe->transfer_size = BUFFER_SIZE;
  1161. pipe->transfer_buffer = kzalloc(pipe->transfer_size,
  1162. GFP_KERNEL);
  1163. if (pipe->transfer_buffer == NULL) {
  1164. DBG("out of memory!\n");
  1165. return -ENOMEM;
  1166. }
  1167. cam->b_acquire = 0;
  1168. cam->frame_count = 0;
  1169. /*** start create system buffers ***/
  1170. for (i = 0; i < FRAMES; i++) {
  1171. /* always allocate maximum size for system buffers */
  1172. cam->buffer.frame[i].lpvbits = vmalloc(MAX_FRAME_SIZE);
  1173. DBG("valloc %p, idx %lu, pdata %p\n",
  1174. &cam->buffer.frame[i], i,
  1175. cam->buffer.frame[i].lpvbits);
  1176. if (cam->buffer.frame[i].lpvbits == NULL) {
  1177. printk(KERN_INFO KBUILD_MODNAME ": out of memory. "
  1178. "Using less frames\n");
  1179. break;
  1180. }
  1181. }
  1182. if (i == 0) {
  1183. printk(KERN_INFO KBUILD_MODNAME ": out of memory. Aborting\n");
  1184. kfree(cam->pipe->transfer_buffer);
  1185. cam->pipe->transfer_buffer = NULL;
  1186. return -ENOMEM;
  1187. } else
  1188. cam->buffer.dwFrames = i;
  1189. /* make sure internal states are set */
  1190. for (i = 0; i < FRAMES; i++) {
  1191. cam->buffer.frame[i].ulState = ZR364XX_READ_IDLE;
  1192. cam->buffer.frame[i].cur_size = 0;
  1193. }
  1194. cam->cur_frame = 0;
  1195. cam->last_frame = -1;
  1196. /*** end create system buffers ***/
  1197. /* start read pipe */
  1198. zr364xx_start_readpipe(cam);
  1199. DBG(": board initialized\n");
  1200. return 0;
  1201. }
  1202. static int zr364xx_probe(struct usb_interface *intf,
  1203. const struct usb_device_id *id)
  1204. {
  1205. struct usb_device *udev = interface_to_usbdev(intf);
  1206. struct zr364xx_camera *cam = NULL;
  1207. struct usb_host_interface *iface_desc;
  1208. struct usb_endpoint_descriptor *endpoint;
  1209. struct v4l2_ctrl_handler *hdl;
  1210. int err;
  1211. int i;
  1212. DBG("probing...\n");
  1213. dev_info(&intf->dev, DRIVER_DESC " compatible webcam plugged\n");
  1214. dev_info(&intf->dev, "model %04x:%04x detected\n",
  1215. le16_to_cpu(udev->descriptor.idVendor),
  1216. le16_to_cpu(udev->descriptor.idProduct));
  1217. cam = kzalloc(sizeof(struct zr364xx_camera), GFP_KERNEL);
  1218. if (cam == NULL) {
  1219. dev_err(&udev->dev, "cam: out of memory !\n");
  1220. return -ENOMEM;
  1221. }
  1222. cam->v4l2_dev.release = zr364xx_release;
  1223. err = v4l2_device_register(&intf->dev, &cam->v4l2_dev);
  1224. if (err < 0) {
  1225. dev_err(&udev->dev, "couldn't register v4l2_device\n");
  1226. kfree(cam);
  1227. return err;
  1228. }
  1229. hdl = &cam->ctrl_handler;
  1230. v4l2_ctrl_handler_init(hdl, 1);
  1231. v4l2_ctrl_new_std(hdl, &zr364xx_ctrl_ops,
  1232. V4L2_CID_BRIGHTNESS, 0, 127, 1, 64);
  1233. if (hdl->error) {
  1234. err = hdl->error;
  1235. dev_err(&udev->dev, "couldn't register control\n");
  1236. goto fail;
  1237. }
  1238. /* save the init method used by this camera */
  1239. cam->method = id->driver_info;
  1240. mutex_init(&cam->lock);
  1241. cam->vdev = zr364xx_template;
  1242. cam->vdev.lock = &cam->lock;
  1243. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1244. cam->vdev.ctrl_handler = &cam->ctrl_handler;
  1245. set_bit(V4L2_FL_USE_FH_PRIO, &cam->vdev.flags);
  1246. video_set_drvdata(&cam->vdev, cam);
  1247. if (debug)
  1248. cam->vdev.debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
  1249. cam->udev = udev;
  1250. switch (mode) {
  1251. case 1:
  1252. dev_info(&udev->dev, "160x120 mode selected\n");
  1253. cam->width = 160;
  1254. cam->height = 120;
  1255. break;
  1256. case 2:
  1257. dev_info(&udev->dev, "640x480 mode selected\n");
  1258. cam->width = 640;
  1259. cam->height = 480;
  1260. break;
  1261. default:
  1262. dev_info(&udev->dev, "320x240 mode selected\n");
  1263. cam->width = 320;
  1264. cam->height = 240;
  1265. break;
  1266. }
  1267. m0d1[0] = mode;
  1268. m1[2].value = 0xf000 + mode;
  1269. m2[1].value = 0xf000 + mode;
  1270. /* special case for METHOD3, the modes are different */
  1271. if (cam->method == METHOD3) {
  1272. switch (mode) {
  1273. case 1:
  1274. m2[1].value = 0xf000 + 4;
  1275. break;
  1276. case 2:
  1277. m2[1].value = 0xf000 + 0;
  1278. break;
  1279. default:
  1280. m2[1].value = 0xf000 + 1;
  1281. break;
  1282. }
  1283. }
  1284. header2[437] = cam->height / 256;
  1285. header2[438] = cam->height % 256;
  1286. header2[439] = cam->width / 256;
  1287. header2[440] = cam->width % 256;
  1288. cam->nb = 0;
  1289. DBG("dev: %p, udev %p interface %p\n", cam, cam->udev, intf);
  1290. /* set up the endpoint information */
  1291. iface_desc = intf->cur_altsetting;
  1292. DBG("num endpoints %d\n", iface_desc->desc.bNumEndpoints);
  1293. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1294. endpoint = &iface_desc->endpoint[i].desc;
  1295. if (!cam->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1296. /* we found the bulk in endpoint */
  1297. cam->read_endpoint = endpoint->bEndpointAddress;
  1298. }
  1299. }
  1300. if (!cam->read_endpoint) {
  1301. err = -ENOMEM;
  1302. dev_err(&intf->dev, "Could not find bulk-in endpoint\n");
  1303. goto fail;
  1304. }
  1305. /* v4l */
  1306. INIT_LIST_HEAD(&cam->vidq.active);
  1307. cam->vidq.cam = cam;
  1308. usb_set_intfdata(intf, cam);
  1309. /* load zr364xx board specific */
  1310. err = zr364xx_board_init(cam);
  1311. if (!err)
  1312. err = v4l2_ctrl_handler_setup(hdl);
  1313. if (err)
  1314. goto fail;
  1315. spin_lock_init(&cam->slock);
  1316. cam->fmt = formats;
  1317. videobuf_queue_vmalloc_init(&cam->vb_vidq, &zr364xx_video_qops,
  1318. NULL, &cam->slock,
  1319. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1320. V4L2_FIELD_NONE,
  1321. sizeof(struct zr364xx_buffer), cam, &cam->lock);
  1322. err = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1323. if (err) {
  1324. dev_err(&udev->dev, "video_register_device failed\n");
  1325. goto fail;
  1326. }
  1327. dev_info(&udev->dev, DRIVER_DESC " controlling device %s\n",
  1328. video_device_node_name(&cam->vdev));
  1329. return 0;
  1330. fail:
  1331. v4l2_ctrl_handler_free(hdl);
  1332. v4l2_device_unregister(&cam->v4l2_dev);
  1333. kfree(cam);
  1334. return err;
  1335. }
  1336. static void zr364xx_disconnect(struct usb_interface *intf)
  1337. {
  1338. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1339. mutex_lock(&cam->lock);
  1340. usb_set_intfdata(intf, NULL);
  1341. dev_info(&intf->dev, DRIVER_DESC " webcam unplugged\n");
  1342. video_unregister_device(&cam->vdev);
  1343. v4l2_device_disconnect(&cam->v4l2_dev);
  1344. /* stops the read pipe if it is running */
  1345. if (cam->b_acquire)
  1346. zr364xx_stop_acquire(cam);
  1347. zr364xx_stop_readpipe(cam);
  1348. mutex_unlock(&cam->lock);
  1349. v4l2_device_put(&cam->v4l2_dev);
  1350. }
  1351. /**********************/
  1352. /* Module integration */
  1353. /**********************/
  1354. static struct usb_driver zr364xx_driver = {
  1355. .name = "zr364xx",
  1356. .probe = zr364xx_probe,
  1357. .disconnect = zr364xx_disconnect,
  1358. .id_table = device_table
  1359. };
  1360. module_usb_driver(zr364xx_driver);
  1361. MODULE_AUTHOR(DRIVER_AUTHOR);
  1362. MODULE_DESCRIPTION(DRIVER_DESC);
  1363. MODULE_LICENSE("GPL");
  1364. MODULE_VERSION(DRIVER_VERSION);