konicawc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * konicawc.c - konica webcam driver
  3. *
  4. * Author: Simon Evans <spse@secret.org.uk>
  5. *
  6. * Copyright (C) 2002 Simon Evans
  7. *
  8. * Licence: GPL
  9. *
  10. * Driver for USB webcams based on Konica chipset. This
  11. * chipset is used in Intel YC76 camera.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/input.h>
  18. #include "usbvideo.h"
  19. #define MAX_BRIGHTNESS 108
  20. #define MAX_CONTRAST 108
  21. #define MAX_SATURATION 108
  22. #define MAX_SHARPNESS 108
  23. #define MAX_WHITEBAL 372
  24. #define MAX_SPEED 6
  25. #define MAX_CAMERAS 1
  26. #define DRIVER_VERSION "v1.4"
  27. #define DRIVER_DESC "Konica Webcam driver"
  28. enum ctrl_req {
  29. SetWhitebal = 0x01,
  30. SetBrightness = 0x02,
  31. SetSharpness = 0x03,
  32. SetContrast = 0x04,
  33. SetSaturation = 0x05,
  34. };
  35. enum frame_sizes {
  36. SIZE_160X120 = 0,
  37. SIZE_160X136 = 1,
  38. SIZE_176X144 = 2,
  39. SIZE_320X240 = 3,
  40. };
  41. #define MAX_FRAME_SIZE SIZE_320X240
  42. static struct usbvideo *cams;
  43. #ifdef CONFIG_USB_DEBUG
  44. static int debug;
  45. #define DEBUG(n, format, arg...) \
  46. if (n <= debug) { \
  47. printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __FUNCTION__ , ## arg); \
  48. }
  49. #else
  50. #define DEBUG(n, arg...)
  51. static const int debug = 0;
  52. #endif
  53. /* Some default values for initial camera settings,
  54. can be set by modprobe */
  55. static int size;
  56. static int speed = 6; /* Speed (fps) 0 (slowest) to 6 (fastest) */
  57. static int brightness = MAX_BRIGHTNESS/2;
  58. static int contrast = MAX_CONTRAST/2;
  59. static int saturation = MAX_SATURATION/2;
  60. static int sharpness = MAX_SHARPNESS/2;
  61. static int whitebal = 3*(MAX_WHITEBAL/4);
  62. static int spd_to_iface[] = { 1, 0, 3, 2, 4, 5, 6 };
  63. /* These FPS speeds are from the windows config box. They are
  64. * indexed on size (0-2) and speed (0-6). Divide by 3 to get the
  65. * real fps.
  66. */
  67. static int spd_to_fps[][7] = { { 24, 40, 48, 60, 72, 80, 100 },
  68. { 24, 40, 48, 60, 72, 80, 100 },
  69. { 18, 30, 36, 45, 54, 60, 75 },
  70. { 6, 10, 12, 15, 18, 21, 25 } };
  71. struct cam_size {
  72. u16 width;
  73. u16 height;
  74. u8 cmd;
  75. };
  76. static struct cam_size camera_sizes[] = { { 160, 120, 0x7 },
  77. { 160, 136, 0xa },
  78. { 176, 144, 0x4 },
  79. { 320, 240, 0x5 } };
  80. struct konicawc {
  81. u8 brightness; /* camera uses 0 - 9, x11 for real value */
  82. u8 contrast; /* as above */
  83. u8 saturation; /* as above */
  84. u8 sharpness; /* as above */
  85. u8 white_bal; /* 0 - 33, x11 for real value */
  86. u8 speed; /* Stored as 0 - 6, used as index in spd_to_* (above) */
  87. u8 size; /* Frame Size */
  88. int height;
  89. int width;
  90. struct urb *sts_urb[USBVIDEO_NUMSBUF];
  91. u8 sts_buf[USBVIDEO_NUMSBUF][FRAMES_PER_DESC];
  92. struct urb *last_data_urb;
  93. int lastframe;
  94. int cur_frame_size; /* number of bytes in current frame size */
  95. int maxline; /* number of lines per frame */
  96. int yplanesz; /* Number of bytes in the Y plane */
  97. unsigned int buttonsts:1;
  98. #ifdef CONFIG_INPUT
  99. struct input_dev input;
  100. char input_physname[64];
  101. #endif
  102. };
  103. #define konicawc_set_misc(uvd, req, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, req, value, index, NULL, 0)
  104. #define konicawc_get_misc(uvd, req, value, index, buf, sz) konicawc_ctrl_msg(uvd, USB_DIR_IN, req, value, index, buf, sz)
  105. #define konicawc_set_value(uvd, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, 2, value, index, NULL, 0)
  106. static int konicawc_ctrl_msg(struct uvd *uvd, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
  107. {
  108. int retval = usb_control_msg(uvd->dev,
  109. dir ? usb_rcvctrlpipe(uvd->dev, 0) : usb_sndctrlpipe(uvd->dev, 0),
  110. request, 0x40 | dir, value, index, buf, len, 1000);
  111. return retval < 0 ? retval : 0;
  112. }
  113. static inline void konicawc_camera_on(struct uvd *uvd)
  114. {
  115. DEBUG(0, "camera on");
  116. konicawc_set_misc(uvd, 0x2, 1, 0x0b);
  117. }
  118. static inline void konicawc_camera_off(struct uvd *uvd)
  119. {
  120. DEBUG(0, "camera off");
  121. konicawc_set_misc(uvd, 0x2, 0, 0x0b);
  122. }
  123. static void konicawc_set_camera_size(struct uvd *uvd)
  124. {
  125. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  126. konicawc_set_misc(uvd, 0x2, camera_sizes[cam->size].cmd, 0x08);
  127. cam->width = camera_sizes[cam->size].width;
  128. cam->height = camera_sizes[cam->size].height;
  129. cam->yplanesz = cam->height * cam->width;
  130. cam->cur_frame_size = (cam->yplanesz * 3) / 2;
  131. cam->maxline = cam->yplanesz / 256;
  132. uvd->videosize = VIDEOSIZE(cam->width, cam->height);
  133. }
  134. static int konicawc_setup_on_open(struct uvd *uvd)
  135. {
  136. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  137. DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
  138. cam->brightness * 11);
  139. konicawc_set_value(uvd, cam->brightness, SetBrightness);
  140. DEBUG(1, "setting white balance to %d (%d)", cam->white_bal,
  141. cam->white_bal * 11);
  142. konicawc_set_value(uvd, cam->white_bal, SetWhitebal);
  143. DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
  144. cam->contrast * 11);
  145. konicawc_set_value(uvd, cam->contrast, SetContrast);
  146. DEBUG(1, "setting saturation to %d (%d)", cam->saturation,
  147. cam->saturation * 11);
  148. konicawc_set_value(uvd, cam->saturation, SetSaturation);
  149. DEBUG(1, "setting sharpness to %d (%d)", cam->sharpness,
  150. cam->sharpness * 11);
  151. konicawc_set_value(uvd, cam->sharpness, SetSharpness);
  152. konicawc_set_camera_size(uvd);
  153. cam->lastframe = -2;
  154. cam->buttonsts = 0;
  155. return 0;
  156. }
  157. static void konicawc_adjust_picture(struct uvd *uvd)
  158. {
  159. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  160. konicawc_camera_off(uvd);
  161. DEBUG(1, "new brightness: %d", uvd->vpic.brightness);
  162. uvd->vpic.brightness = (uvd->vpic.brightness > MAX_BRIGHTNESS) ? MAX_BRIGHTNESS : uvd->vpic.brightness;
  163. if(cam->brightness != uvd->vpic.brightness / 11) {
  164. cam->brightness = uvd->vpic.brightness / 11;
  165. DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
  166. cam->brightness * 11);
  167. konicawc_set_value(uvd, cam->brightness, SetBrightness);
  168. }
  169. DEBUG(1, "new contrast: %d", uvd->vpic.contrast);
  170. uvd->vpic.contrast = (uvd->vpic.contrast > MAX_CONTRAST) ? MAX_CONTRAST : uvd->vpic.contrast;
  171. if(cam->contrast != uvd->vpic.contrast / 11) {
  172. cam->contrast = uvd->vpic.contrast / 11;
  173. DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
  174. cam->contrast * 11);
  175. konicawc_set_value(uvd, cam->contrast, SetContrast);
  176. }
  177. konicawc_camera_on(uvd);
  178. }
  179. static int konicawc_compress_iso(struct uvd *uvd, struct urb *dataurb, struct urb *stsurb)
  180. {
  181. char *cdata;
  182. int i, totlen = 0;
  183. unsigned char *status = stsurb->transfer_buffer;
  184. int keep = 0, discard = 0, bad = 0;
  185. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  186. for (i = 0; i < dataurb->number_of_packets; i++) {
  187. int button = cam->buttonsts;
  188. unsigned char sts;
  189. int n = dataurb->iso_frame_desc[i].actual_length;
  190. int st = dataurb->iso_frame_desc[i].status;
  191. cdata = dataurb->transfer_buffer +
  192. dataurb->iso_frame_desc[i].offset;
  193. /* Detect and ignore errored packets */
  194. if (st < 0) {
  195. DEBUG(1, "Data error: packet=%d. len=%d. status=%d.",
  196. i, n, st);
  197. uvd->stats.iso_err_count++;
  198. continue;
  199. }
  200. /* Detect and ignore empty packets */
  201. if (n <= 0) {
  202. uvd->stats.iso_skip_count++;
  203. continue;
  204. }
  205. /* See what the status data said about the packet */
  206. sts = *(status+stsurb->iso_frame_desc[i].offset);
  207. /* sts: 0x80-0xff: frame start with frame number (ie 0-7f)
  208. * otherwise:
  209. * bit 0 0: keep packet
  210. * 1: drop packet (padding data)
  211. *
  212. * bit 4 0 button not clicked
  213. * 1 button clicked
  214. * button is used to `take a picture' (in software)
  215. */
  216. if(sts < 0x80) {
  217. button = !!(sts & 0x40);
  218. sts &= ~0x40;
  219. }
  220. /* work out the button status, but don't do
  221. anything with it for now */
  222. if(button != cam->buttonsts) {
  223. DEBUG(2, "button: %sclicked", button ? "" : "un");
  224. cam->buttonsts = button;
  225. #ifdef CONFIG_INPUT
  226. input_report_key(&cam->input, BTN_0, cam->buttonsts);
  227. input_sync(&cam->input);
  228. #endif
  229. }
  230. if(sts == 0x01) { /* drop frame */
  231. discard++;
  232. continue;
  233. }
  234. if((sts > 0x01) && (sts < 0x80)) {
  235. info("unknown status %2.2x", sts);
  236. bad++;
  237. continue;
  238. }
  239. if(!sts && cam->lastframe == -2) {
  240. DEBUG(2, "dropping frame looking for image start");
  241. continue;
  242. }
  243. keep++;
  244. if(sts & 0x80) { /* frame start */
  245. unsigned char marker[] = { 0, 0xff, 0, 0x00 };
  246. if(cam->lastframe == -2) {
  247. DEBUG(2, "found initial image");
  248. cam->lastframe = -1;
  249. }
  250. marker[3] = sts & 0x7F;
  251. RingQueue_Enqueue(&uvd->dp, marker, 4);
  252. totlen += 4;
  253. }
  254. totlen += n; /* Little local accounting */
  255. RingQueue_Enqueue(&uvd->dp, cdata, n);
  256. }
  257. DEBUG(8, "finished: keep = %d discard = %d bad = %d added %d bytes",
  258. keep, discard, bad, totlen);
  259. return totlen;
  260. }
  261. static void resubmit_urb(struct uvd *uvd, struct urb *urb)
  262. {
  263. int i, ret;
  264. for (i = 0; i < FRAMES_PER_DESC; i++) {
  265. urb->iso_frame_desc[i].status = 0;
  266. }
  267. urb->dev = uvd->dev;
  268. urb->status = 0;
  269. ret = usb_submit_urb(urb, GFP_ATOMIC);
  270. DEBUG(3, "submitting urb of length %d", urb->transfer_buffer_length);
  271. if(ret)
  272. err("usb_submit_urb error (%d)", ret);
  273. }
  274. static void konicawc_isoc_irq(struct urb *urb, struct pt_regs *regs)
  275. {
  276. struct uvd *uvd = urb->context;
  277. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  278. /* We don't want to do anything if we are about to be removed! */
  279. if (!CAMERA_IS_OPERATIONAL(uvd))
  280. return;
  281. if (!uvd->streaming) {
  282. DEBUG(1, "Not streaming, but interrupt!");
  283. return;
  284. }
  285. DEBUG(3, "got frame %d len = %d buflen =%d", urb->start_frame, urb->actual_length, urb->transfer_buffer_length);
  286. uvd->stats.urb_count++;
  287. if (urb->transfer_buffer_length > 32) {
  288. cam->last_data_urb = urb;
  289. return;
  290. }
  291. /* Copy the data received into ring queue */
  292. if(cam->last_data_urb) {
  293. int len = 0;
  294. if(urb->start_frame != cam->last_data_urb->start_frame)
  295. err("Lost sync on frames");
  296. else if (!urb->status && !cam->last_data_urb->status)
  297. len = konicawc_compress_iso(uvd, cam->last_data_urb, urb);
  298. resubmit_urb(uvd, cam->last_data_urb);
  299. resubmit_urb(uvd, urb);
  300. cam->last_data_urb = NULL;
  301. uvd->stats.urb_length = len;
  302. uvd->stats.data_count += len;
  303. if(len)
  304. RingQueue_WakeUpInterruptible(&uvd->dp);
  305. return;
  306. }
  307. return;
  308. }
  309. static int konicawc_start_data(struct uvd *uvd)
  310. {
  311. struct usb_device *dev = uvd->dev;
  312. int i, errFlag;
  313. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  314. int pktsz;
  315. struct usb_interface *intf;
  316. struct usb_host_interface *interface = NULL;
  317. intf = usb_ifnum_to_if(dev, uvd->iface);
  318. if (intf)
  319. interface = usb_altnum_to_altsetting(intf,
  320. spd_to_iface[cam->speed]);
  321. if (!interface)
  322. return -ENXIO;
  323. pktsz = le16_to_cpu(interface->endpoint[1].desc.wMaxPacketSize);
  324. DEBUG(1, "pktsz = %d", pktsz);
  325. if (!CAMERA_IS_OPERATIONAL(uvd)) {
  326. err("Camera is not operational");
  327. return -EFAULT;
  328. }
  329. uvd->curframe = -1;
  330. konicawc_camera_on(uvd);
  331. /* Alternate interface 1 is is the biggest frame size */
  332. i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
  333. if (i < 0) {
  334. err("usb_set_interface error");
  335. uvd->last_error = i;
  336. return -EBUSY;
  337. }
  338. /* We double buffer the Iso lists */
  339. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  340. int j, k;
  341. struct urb *urb = uvd->sbuf[i].urb;
  342. urb->dev = dev;
  343. urb->context = uvd;
  344. urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
  345. urb->interval = 1;
  346. urb->transfer_flags = URB_ISO_ASAP;
  347. urb->transfer_buffer = uvd->sbuf[i].data;
  348. urb->complete = konicawc_isoc_irq;
  349. urb->number_of_packets = FRAMES_PER_DESC;
  350. urb->transfer_buffer_length = pktsz * FRAMES_PER_DESC;
  351. for (j=k=0; j < FRAMES_PER_DESC; j++, k += pktsz) {
  352. urb->iso_frame_desc[j].offset = k;
  353. urb->iso_frame_desc[j].length = pktsz;
  354. }
  355. urb = cam->sts_urb[i];
  356. urb->dev = dev;
  357. urb->context = uvd;
  358. urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp-1);
  359. urb->interval = 1;
  360. urb->transfer_flags = URB_ISO_ASAP;
  361. urb->transfer_buffer = cam->sts_buf[i];
  362. urb->complete = konicawc_isoc_irq;
  363. urb->number_of_packets = FRAMES_PER_DESC;
  364. urb->transfer_buffer_length = FRAMES_PER_DESC;
  365. for (j=0; j < FRAMES_PER_DESC; j++) {
  366. urb->iso_frame_desc[j].offset = j;
  367. urb->iso_frame_desc[j].length = 1;
  368. }
  369. }
  370. cam->last_data_urb = NULL;
  371. /* Submit all URBs */
  372. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  373. errFlag = usb_submit_urb(cam->sts_urb[i], GFP_KERNEL);
  374. if (errFlag)
  375. err("usb_submit_isoc(%d) ret %d", i, errFlag);
  376. errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
  377. if (errFlag)
  378. err ("usb_submit_isoc(%d) ret %d", i, errFlag);
  379. }
  380. uvd->streaming = 1;
  381. DEBUG(1, "streaming=1 video_endp=$%02x", uvd->video_endp);
  382. return 0;
  383. }
  384. static void konicawc_stop_data(struct uvd *uvd)
  385. {
  386. int i, j;
  387. struct konicawc *cam;
  388. if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
  389. return;
  390. konicawc_camera_off(uvd);
  391. uvd->streaming = 0;
  392. cam = (struct konicawc *)uvd->user_data;
  393. cam->last_data_urb = NULL;
  394. /* Unschedule all of the iso td's */
  395. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  396. usb_kill_urb(uvd->sbuf[i].urb);
  397. usb_kill_urb(cam->sts_urb[i]);
  398. }
  399. if (!uvd->remove_pending) {
  400. /* Set packet size to 0 */
  401. j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
  402. if (j < 0) {
  403. err("usb_set_interface() error %d.", j);
  404. uvd->last_error = j;
  405. }
  406. }
  407. }
  408. static void konicawc_process_isoc(struct uvd *uvd, struct usbvideo_frame *frame)
  409. {
  410. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  411. int maxline = cam->maxline;
  412. int yplanesz = cam->yplanesz;
  413. assert(frame != NULL);
  414. DEBUG(5, "maxline = %d yplanesz = %d", maxline, yplanesz);
  415. DEBUG(3, "Frame state = %d", frame->scanstate);
  416. if(frame->scanstate == ScanState_Scanning) {
  417. int drop = 0;
  418. int curframe;
  419. int fdrops = 0;
  420. DEBUG(3, "Searching for marker, queue len = %d", RingQueue_GetLength(&uvd->dp));
  421. while(RingQueue_GetLength(&uvd->dp) >= 4) {
  422. if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
  423. (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xff) &&
  424. (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00) &&
  425. (RING_QUEUE_PEEK(&uvd->dp, 3) < 0x80)) {
  426. curframe = RING_QUEUE_PEEK(&uvd->dp, 3);
  427. if(cam->lastframe >= 0) {
  428. fdrops = (0x80 + curframe - cam->lastframe) & 0x7F;
  429. fdrops--;
  430. if(fdrops) {
  431. info("Dropped %d frames (%d -> %d)", fdrops,
  432. cam->lastframe, curframe);
  433. }
  434. }
  435. cam->lastframe = curframe;
  436. frame->curline = 0;
  437. frame->scanstate = ScanState_Lines;
  438. RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 4);
  439. break;
  440. }
  441. RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
  442. drop++;
  443. }
  444. if(drop)
  445. DEBUG(2, "dropped %d bytes looking for new frame", drop);
  446. }
  447. if(frame->scanstate == ScanState_Scanning)
  448. return;
  449. /* Try to move data from queue into frame buffer
  450. * We get data in blocks of 384 bytes made up of:
  451. * 256 Y, 64 U, 64 V.
  452. * This needs to be written out as a Y plane, a U plane and a V plane.
  453. */
  454. while ( frame->curline < maxline && (RingQueue_GetLength(&uvd->dp) >= 384)) {
  455. /* Y */
  456. RingQueue_Dequeue(&uvd->dp, frame->data + (frame->curline * 256), 256);
  457. /* U */
  458. RingQueue_Dequeue(&uvd->dp, frame->data + yplanesz + (frame->curline * 64), 64);
  459. /* V */
  460. RingQueue_Dequeue(&uvd->dp, frame->data + (5 * yplanesz)/4 + (frame->curline * 64), 64);
  461. frame->seqRead_Length += 384;
  462. frame->curline++;
  463. }
  464. /* See if we filled the frame */
  465. if (frame->curline == maxline) {
  466. DEBUG(5, "got whole frame");
  467. frame->frameState = FrameState_Done_Hold;
  468. frame->curline = 0;
  469. uvd->curframe = -1;
  470. uvd->stats.frame_num++;
  471. }
  472. }
  473. static int konicawc_find_fps(int size, int fps)
  474. {
  475. int i;
  476. fps *= 3;
  477. DEBUG(1, "konica_find_fps: size = %d fps = %d", size, fps);
  478. if(fps <= spd_to_fps[size][0])
  479. return 0;
  480. if(fps >= spd_to_fps[size][MAX_SPEED])
  481. return MAX_SPEED;
  482. for(i = 0; i < MAX_SPEED; i++) {
  483. if((fps >= spd_to_fps[size][i]) && (fps <= spd_to_fps[size][i+1])) {
  484. DEBUG(2, "fps %d between %d and %d", fps, i, i+1);
  485. if( (fps - spd_to_fps[size][i]) < (spd_to_fps[size][i+1] - fps))
  486. return i;
  487. else
  488. return i+1;
  489. }
  490. }
  491. return MAX_SPEED+1;
  492. }
  493. static int konicawc_set_video_mode(struct uvd *uvd, struct video_window *vw)
  494. {
  495. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  496. int newspeed = cam->speed;
  497. int newsize;
  498. int x = vw->width;
  499. int y = vw->height;
  500. int fps = vw->flags;
  501. if(x > 0 && y > 0) {
  502. DEBUG(2, "trying to find size %d,%d", x, y);
  503. for(newsize = 0; newsize <= MAX_FRAME_SIZE; newsize++) {
  504. if((camera_sizes[newsize].width == x) && (camera_sizes[newsize].height == y))
  505. break;
  506. }
  507. } else {
  508. newsize = cam->size;
  509. }
  510. if(newsize > MAX_FRAME_SIZE) {
  511. DEBUG(1, "couldn't find size %d,%d", x, y);
  512. return -EINVAL;
  513. }
  514. if(fps > 0) {
  515. DEBUG(1, "trying to set fps to %d", fps);
  516. newspeed = konicawc_find_fps(newsize, fps);
  517. DEBUG(1, "find_fps returned %d (%d)", newspeed, spd_to_fps[newsize][newspeed]);
  518. }
  519. if(newspeed > MAX_SPEED)
  520. return -EINVAL;
  521. DEBUG(1, "setting size to %d speed to %d", newsize, newspeed);
  522. if((newsize == cam->size) && (newspeed == cam->speed)) {
  523. DEBUG(1, "Nothing to do");
  524. return 0;
  525. }
  526. DEBUG(0, "setting to %dx%d @ %d fps", camera_sizes[newsize].width,
  527. camera_sizes[newsize].height, spd_to_fps[newsize][newspeed]/3);
  528. konicawc_stop_data(uvd);
  529. uvd->ifaceAltActive = spd_to_iface[newspeed];
  530. DEBUG(1, "new interface = %d", uvd->ifaceAltActive);
  531. cam->speed = newspeed;
  532. if(cam->size != newsize) {
  533. cam->size = newsize;
  534. konicawc_set_camera_size(uvd);
  535. }
  536. /* Flush the input queue and clear any current frame in progress */
  537. RingQueue_Flush(&uvd->dp);
  538. cam->lastframe = -2;
  539. if(uvd->curframe != -1) {
  540. uvd->frame[uvd->curframe].curline = 0;
  541. uvd->frame[uvd->curframe].seqRead_Length = 0;
  542. uvd->frame[uvd->curframe].seqRead_Index = 0;
  543. }
  544. konicawc_start_data(uvd);
  545. return 0;
  546. }
  547. static int konicawc_calculate_fps(struct uvd *uvd)
  548. {
  549. struct konicawc *cam = uvd->user_data;
  550. return spd_to_fps[cam->size][cam->speed]/3;
  551. }
  552. static void konicawc_configure_video(struct uvd *uvd)
  553. {
  554. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  555. u8 buf[2];
  556. memset(&uvd->vpic, 0, sizeof(uvd->vpic));
  557. memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
  558. RESTRICT_TO_RANGE(brightness, 0, MAX_BRIGHTNESS);
  559. RESTRICT_TO_RANGE(contrast, 0, MAX_CONTRAST);
  560. RESTRICT_TO_RANGE(saturation, 0, MAX_SATURATION);
  561. RESTRICT_TO_RANGE(sharpness, 0, MAX_SHARPNESS);
  562. RESTRICT_TO_RANGE(whitebal, 0, MAX_WHITEBAL);
  563. cam->brightness = brightness / 11;
  564. cam->contrast = contrast / 11;
  565. cam->saturation = saturation / 11;
  566. cam->sharpness = sharpness / 11;
  567. cam->white_bal = whitebal / 11;
  568. uvd->vpic.colour = 108;
  569. uvd->vpic.hue = 108;
  570. uvd->vpic.brightness = brightness;
  571. uvd->vpic.contrast = contrast;
  572. uvd->vpic.whiteness = whitebal;
  573. uvd->vpic.depth = 6;
  574. uvd->vpic.palette = VIDEO_PALETTE_YUV420P;
  575. memset(&uvd->vcap, 0, sizeof(uvd->vcap));
  576. strcpy(uvd->vcap.name, "Konica Webcam");
  577. uvd->vcap.type = VID_TYPE_CAPTURE;
  578. uvd->vcap.channels = 1;
  579. uvd->vcap.audios = 0;
  580. uvd->vcap.minwidth = camera_sizes[SIZE_160X120].width;
  581. uvd->vcap.minheight = camera_sizes[SIZE_160X120].height;
  582. uvd->vcap.maxwidth = camera_sizes[SIZE_320X240].width;
  583. uvd->vcap.maxheight = camera_sizes[SIZE_320X240].height;
  584. memset(&uvd->vchan, 0, sizeof(uvd->vchan));
  585. uvd->vchan.flags = 0 ;
  586. uvd->vchan.tuners = 0;
  587. uvd->vchan.channel = 0;
  588. uvd->vchan.type = VIDEO_TYPE_CAMERA;
  589. strcpy(uvd->vchan.name, "Camera");
  590. /* Talk to device */
  591. DEBUG(1, "device init");
  592. if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
  593. DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
  594. if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
  595. DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
  596. if(konicawc_set_misc(uvd, 0x2, 0, 0xd))
  597. DEBUG(2, "2,0,d failed");
  598. DEBUG(1, "setting initial values");
  599. }
  600. static int konicawc_probe(struct usb_interface *intf, const struct usb_device_id *devid)
  601. {
  602. struct usb_device *dev = interface_to_usbdev(intf);
  603. struct uvd *uvd = NULL;
  604. int ix, i, nas;
  605. int actInterface=-1, inactInterface=-1, maxPS=0;
  606. unsigned char video_ep = 0;
  607. DEBUG(1, "konicawc_probe(%p)", intf);
  608. /* We don't handle multi-config cameras */
  609. if (dev->descriptor.bNumConfigurations != 1)
  610. return -ENODEV;
  611. info("Konica Webcam (rev. 0x%04x)", le16_to_cpu(dev->descriptor.bcdDevice));
  612. RESTRICT_TO_RANGE(speed, 0, MAX_SPEED);
  613. /* Validate found interface: must have one ISO endpoint */
  614. nas = intf->num_altsetting;
  615. if (nas != 8) {
  616. err("Incorrect number of alternate settings (%d) for this camera!", nas);
  617. return -ENODEV;
  618. }
  619. /* Validate all alternate settings */
  620. for (ix=0; ix < nas; ix++) {
  621. const struct usb_host_interface *interface;
  622. const struct usb_endpoint_descriptor *endpoint;
  623. interface = &intf->altsetting[ix];
  624. i = interface->desc.bAlternateSetting;
  625. if (interface->desc.bNumEndpoints != 2) {
  626. err("Interface %d. has %u. endpoints!",
  627. interface->desc.bInterfaceNumber,
  628. (unsigned)(interface->desc.bNumEndpoints));
  629. return -ENODEV;
  630. }
  631. endpoint = &interface->endpoint[1].desc;
  632. DEBUG(1, "found endpoint: addr: 0x%2.2x maxps = 0x%4.4x",
  633. endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize));
  634. if (video_ep == 0)
  635. video_ep = endpoint->bEndpointAddress;
  636. else if (video_ep != endpoint->bEndpointAddress) {
  637. err("Alternate settings have different endpoint addresses!");
  638. return -ENODEV;
  639. }
  640. if ((endpoint->bmAttributes & 0x03) != 0x01) {
  641. err("Interface %d. has non-ISO endpoint!",
  642. interface->desc.bInterfaceNumber);
  643. return -ENODEV;
  644. }
  645. if ((endpoint->bEndpointAddress & 0x80) == 0) {
  646. err("Interface %d. has ISO OUT endpoint!",
  647. interface->desc.bInterfaceNumber);
  648. return -ENODEV;
  649. }
  650. if (le16_to_cpu(endpoint->wMaxPacketSize) == 0) {
  651. if (inactInterface < 0)
  652. inactInterface = i;
  653. else {
  654. err("More than one inactive alt. setting!");
  655. return -ENODEV;
  656. }
  657. } else {
  658. if (i == spd_to_iface[speed]) {
  659. /* This one is the requested one */
  660. actInterface = i;
  661. }
  662. }
  663. if (le16_to_cpu(endpoint->wMaxPacketSize) > maxPS)
  664. maxPS = le16_to_cpu(endpoint->wMaxPacketSize);
  665. }
  666. if(actInterface == -1) {
  667. err("Cant find required endpoint");
  668. return -ENODEV;
  669. }
  670. DEBUG(1, "Selecting requested active setting=%d. maxPS=%d.", actInterface, maxPS);
  671. uvd = usbvideo_AllocateDevice(cams);
  672. if (uvd != NULL) {
  673. struct konicawc *cam = (struct konicawc *)(uvd->user_data);
  674. /* Here uvd is a fully allocated uvd object */
  675. for(i = 0; i < USBVIDEO_NUMSBUF; i++) {
  676. cam->sts_urb[i] = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
  677. if(cam->sts_urb[i] == NULL) {
  678. while(i--) {
  679. usb_free_urb(cam->sts_urb[i]);
  680. }
  681. err("can't allocate urbs");
  682. return -ENOMEM;
  683. }
  684. }
  685. cam->speed = speed;
  686. RESTRICT_TO_RANGE(size, SIZE_160X120, SIZE_320X240);
  687. cam->width = camera_sizes[size].width;
  688. cam->height = camera_sizes[size].height;
  689. cam->size = size;
  690. uvd->flags = 0;
  691. uvd->debug = debug;
  692. uvd->dev = dev;
  693. uvd->iface = intf->altsetting->desc.bInterfaceNumber;
  694. uvd->ifaceAltInactive = inactInterface;
  695. uvd->ifaceAltActive = actInterface;
  696. uvd->video_endp = video_ep;
  697. uvd->iso_packet_len = maxPS;
  698. uvd->paletteBits = 1L << VIDEO_PALETTE_YUV420P;
  699. uvd->defaultPalette = VIDEO_PALETTE_YUV420P;
  700. uvd->canvas = VIDEOSIZE(320, 240);
  701. uvd->videosize = VIDEOSIZE(cam->width, cam->height);
  702. /* Initialize konicawc specific data */
  703. konicawc_configure_video(uvd);
  704. i = usbvideo_RegisterVideoDevice(uvd);
  705. uvd->max_frame_size = (320 * 240 * 3)/2;
  706. if (i != 0) {
  707. err("usbvideo_RegisterVideoDevice() failed.");
  708. uvd = NULL;
  709. }
  710. #ifdef CONFIG_INPUT
  711. /* Register input device for button */
  712. memset(&cam->input, 0, sizeof(struct input_dev));
  713. cam->input.name = "Konicawc snapshot button";
  714. cam->input.private = cam;
  715. cam->input.evbit[0] = BIT(EV_KEY);
  716. cam->input.keybit[LONG(BTN_0)] = BIT(BTN_0);
  717. cam->input.id.bustype = BUS_USB;
  718. cam->input.id.vendor = le16_to_cpu(dev->descriptor.idVendor);
  719. cam->input.id.product = le16_to_cpu(dev->descriptor.idProduct);
  720. cam->input.id.version = le16_to_cpu(dev->descriptor.bcdDevice);
  721. input_register_device(&cam->input);
  722. usb_make_path(dev, cam->input_physname, 56);
  723. strcat(cam->input_physname, "/input0");
  724. cam->input.phys = cam->input_physname;
  725. info("konicawc: %s on %s\n", cam->input.name, cam->input.phys);
  726. #endif
  727. }
  728. if (uvd) {
  729. usb_set_intfdata (intf, uvd);
  730. return 0;
  731. }
  732. return -EIO;
  733. }
  734. static void konicawc_free_uvd(struct uvd *uvd)
  735. {
  736. int i;
  737. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  738. #ifdef CONFIG_INPUT
  739. input_unregister_device(&cam->input);
  740. #endif
  741. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  742. usb_free_urb(cam->sts_urb[i]);
  743. cam->sts_urb[i] = NULL;
  744. }
  745. }
  746. static struct usb_device_id id_table[] = {
  747. { USB_DEVICE(0x04c8, 0x0720) }, /* Intel YC 76 */
  748. { } /* Terminating entry */
  749. };
  750. static int __init konicawc_init(void)
  751. {
  752. struct usbvideo_cb cbTbl;
  753. info(DRIVER_DESC " " DRIVER_VERSION);
  754. memset(&cbTbl, 0, sizeof(cbTbl));
  755. cbTbl.probe = konicawc_probe;
  756. cbTbl.setupOnOpen = konicawc_setup_on_open;
  757. cbTbl.processData = konicawc_process_isoc;
  758. cbTbl.getFPS = konicawc_calculate_fps;
  759. cbTbl.setVideoMode = konicawc_set_video_mode;
  760. cbTbl.startDataPump = konicawc_start_data;
  761. cbTbl.stopDataPump = konicawc_stop_data;
  762. cbTbl.adjustPicture = konicawc_adjust_picture;
  763. cbTbl.userFree = konicawc_free_uvd;
  764. return usbvideo_register(
  765. &cams,
  766. MAX_CAMERAS,
  767. sizeof(struct konicawc),
  768. "konicawc",
  769. &cbTbl,
  770. THIS_MODULE,
  771. id_table);
  772. }
  773. static void __exit konicawc_cleanup(void)
  774. {
  775. usbvideo_Deregister(&cams);
  776. }
  777. MODULE_DEVICE_TABLE(usb, id_table);
  778. MODULE_LICENSE("GPL");
  779. MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
  780. MODULE_DESCRIPTION(DRIVER_DESC);
  781. module_param(speed, int, 0);
  782. MODULE_PARM_DESC(speed, "Initial speed: 0 (slowest) - 6 (fastest)");
  783. module_param(size, int, 0);
  784. MODULE_PARM_DESC(size, "Initial Size 0: 160x120 1: 160x136 2: 176x144 3: 320x240");
  785. module_param(brightness, int, 0);
  786. MODULE_PARM_DESC(brightness, "Initial brightness 0 - 108");
  787. module_param(contrast, int, 0);
  788. MODULE_PARM_DESC(contrast, "Initial contrast 0 - 108");
  789. module_param(saturation, int, 0);
  790. MODULE_PARM_DESC(saturation, "Initial saturation 0 - 108");
  791. module_param(sharpness, int, 0);
  792. MODULE_PARM_DESC(sharpness, "Initial brightness 0 - 108");
  793. module_param(whitebal, int, 0);
  794. MODULE_PARM_DESC(whitebal, "Initial white balance 0 - 363");
  795. #ifdef CONFIG_USB_DEBUG
  796. module_param(debug, int, S_IRUGO | S_IWUSR);
  797. MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
  798. #endif
  799. module_init(konicawc_init);
  800. module_exit(konicawc_cleanup);