benq.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Benq DC E300 subdriver
  3. *
  4. * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #define MODULE_NAME "benq"
  21. #include "gspca.h"
  22. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  23. MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
  24. MODULE_LICENSE("GPL");
  25. /* specific webcam descriptor */
  26. struct sd {
  27. struct gspca_dev gspca_dev; /* !! must be the first item */
  28. };
  29. /* V4L2 controls supported by the driver */
  30. static const struct ctrl sd_ctrls[] = {
  31. };
  32. static const struct v4l2_pix_format vga_mode[] = {
  33. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  34. .bytesperline = 320,
  35. .sizeimage = 320 * 240 * 3 / 8 + 590,
  36. .colorspace = V4L2_COLORSPACE_JPEG},
  37. };
  38. static void sd_isoc_irq(struct urb *urb);
  39. /* -- write a register -- */
  40. static void reg_w(struct gspca_dev *gspca_dev,
  41. u16 value, u16 index)
  42. {
  43. struct usb_device *dev = gspca_dev->dev;
  44. int ret;
  45. if (gspca_dev->usb_err < 0)
  46. return;
  47. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  48. 0x02,
  49. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  50. value,
  51. index,
  52. NULL,
  53. 0,
  54. 500);
  55. if (ret < 0) {
  56. err("reg_w err %d", ret);
  57. gspca_dev->usb_err = ret;
  58. }
  59. }
  60. /* this function is called at probe time */
  61. static int sd_config(struct gspca_dev *gspca_dev,
  62. const struct usb_device_id *id)
  63. {
  64. gspca_dev->cam.cam_mode = vga_mode;
  65. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  66. gspca_dev->cam.no_urb_create = 1;
  67. gspca_dev->cam.reverse_alts = 1;
  68. return 0;
  69. }
  70. /* this function is called at probe and resume time */
  71. static int sd_init(struct gspca_dev *gspca_dev)
  72. {
  73. return 0;
  74. }
  75. static int sd_isoc_init(struct gspca_dev *gspca_dev)
  76. {
  77. int ret;
  78. ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface,
  79. gspca_dev->nbalt - 1);
  80. if (ret < 0) {
  81. err("usb_set_interface failed");
  82. return ret;
  83. }
  84. /* reg_w(gspca_dev, 0x0003, 0x0002); */
  85. return 0;
  86. }
  87. /* -- start the camera -- */
  88. static int sd_start(struct gspca_dev *gspca_dev)
  89. {
  90. struct urb *urb;
  91. int i, n;
  92. /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
  93. #if MAX_NURBS < 4
  94. #error "Not enough URBs in the gspca table"
  95. #endif
  96. #define SD_PKT_SZ 64
  97. #define SD_NPKT 32
  98. for (n = 0; n < 4; n++) {
  99. urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
  100. if (!urb) {
  101. err("usb_alloc_urb failed");
  102. return -ENOMEM;
  103. }
  104. gspca_dev->urb[n] = urb;
  105. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  106. SD_PKT_SZ * SD_NPKT,
  107. GFP_KERNEL,
  108. &urb->transfer_dma);
  109. if (urb->transfer_buffer == NULL) {
  110. err("usb_alloc_coherent failed");
  111. return -ENOMEM;
  112. }
  113. urb->dev = gspca_dev->dev;
  114. urb->context = gspca_dev;
  115. urb->transfer_buffer_length = SD_PKT_SZ * SD_NPKT;
  116. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  117. n & 1 ? 0x82 : 0x83);
  118. urb->transfer_flags = URB_ISO_ASAP
  119. | URB_NO_TRANSFER_DMA_MAP;
  120. urb->interval = 1;
  121. urb->complete = sd_isoc_irq;
  122. urb->number_of_packets = SD_NPKT;
  123. for (i = 0; i < SD_NPKT; i++) {
  124. urb->iso_frame_desc[i].length = SD_PKT_SZ;
  125. urb->iso_frame_desc[i].offset = SD_PKT_SZ * i;
  126. }
  127. }
  128. return gspca_dev->usb_err;
  129. }
  130. static void sd_stopN(struct gspca_dev *gspca_dev)
  131. {
  132. reg_w(gspca_dev, 0x003c, 0x0003);
  133. reg_w(gspca_dev, 0x003c, 0x0004);
  134. reg_w(gspca_dev, 0x003c, 0x0005);
  135. reg_w(gspca_dev, 0x003c, 0x0006);
  136. reg_w(gspca_dev, 0x003c, 0x0007);
  137. usb_set_interface(gspca_dev->dev, gspca_dev->iface,
  138. gspca_dev->nbalt - 1);
  139. }
  140. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  141. u8 *data, /* isoc packet */
  142. int len) /* iso packet length */
  143. {
  144. /* unused */
  145. }
  146. /* reception of an URB */
  147. static void sd_isoc_irq(struct urb *urb)
  148. {
  149. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  150. struct urb *urb0;
  151. u8 *data;
  152. int i, st;
  153. PDEBUG(D_PACK, "sd isoc irq");
  154. if (!gspca_dev->streaming)
  155. return;
  156. if (urb->status != 0) {
  157. if (urb->status == -ESHUTDOWN)
  158. return; /* disconnection */
  159. #ifdef CONFIG_PM
  160. if (gspca_dev->frozen)
  161. return;
  162. #endif
  163. err("urb status: %d", urb->status);
  164. return;
  165. }
  166. /* if this is a control URN (ep 0x83), wait */
  167. if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2])
  168. return;
  169. /* scan both received URBs */
  170. if (urb == gspca_dev->urb[1])
  171. urb0 = gspca_dev->urb[0];
  172. else
  173. urb0 = gspca_dev->urb[2];
  174. for (i = 0; i < urb->number_of_packets; i++) {
  175. /* check the packet status and length */
  176. if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ
  177. || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) {
  178. PDEBUG(D_ERR, "ISOC bad lengths %d / %d",
  179. urb0->iso_frame_desc[i].actual_length,
  180. urb->iso_frame_desc[i].actual_length);
  181. gspca_dev->last_packet_type = DISCARD_PACKET;
  182. continue;
  183. }
  184. st = urb0->iso_frame_desc[i].status;
  185. if (st == 0)
  186. st = urb->iso_frame_desc[i].status;
  187. if (st) {
  188. err("ISOC data error: [%d] status=%d",
  189. i, st);
  190. gspca_dev->last_packet_type = DISCARD_PACKET;
  191. continue;
  192. }
  193. /*
  194. * The images are received in URBs of different endpoints
  195. * (0x83 and 0x82).
  196. * Image pieces in URBs of ep 0x83 are continuated in URBs of
  197. * ep 0x82 of the same index.
  198. * The packets in the URBs of endpoint 0x83 start with:
  199. * - 80 ba/bb 00 00 = start of image followed by 'ff d8'
  200. * - 04 ba/bb oo oo = image piece
  201. * where 'oo oo' is the image offset
  202. (not cheked)
  203. * - (other -> bad frame)
  204. * The images are JPEG encoded with full header and
  205. * normal ff escape.
  206. * The end of image ('ff d9') may occur in any URB.
  207. * (not cheked)
  208. */
  209. data = (u8 *) urb0->transfer_buffer
  210. + urb0->iso_frame_desc[i].offset;
  211. if (data[0] == 0x80 && (data[1] & 0xfe) == 0xba) {
  212. /* new image */
  213. gspca_frame_add(gspca_dev, LAST_PACKET,
  214. NULL, 0);
  215. gspca_frame_add(gspca_dev, FIRST_PACKET,
  216. data + 4, SD_PKT_SZ - 4);
  217. } else if (data[0] == 0x04 && (data[1] & 0xfe) == 0xba) {
  218. gspca_frame_add(gspca_dev, INTER_PACKET,
  219. data + 4, SD_PKT_SZ - 4);
  220. } else {
  221. gspca_dev->last_packet_type = DISCARD_PACKET;
  222. continue;
  223. }
  224. data = (u8 *) urb->transfer_buffer
  225. + urb->iso_frame_desc[i].offset;
  226. gspca_frame_add(gspca_dev, INTER_PACKET,
  227. data, SD_PKT_SZ);
  228. }
  229. /* resubmit the URBs */
  230. st = usb_submit_urb(urb0, GFP_ATOMIC);
  231. if (st < 0)
  232. err("usb_submit_urb(0) ret %d", st);
  233. st = usb_submit_urb(urb, GFP_ATOMIC);
  234. if (st < 0)
  235. err("usb_submit_urb() ret %d", st);
  236. }
  237. /* sub-driver description */
  238. static const struct sd_desc sd_desc = {
  239. .name = MODULE_NAME,
  240. .ctrls = sd_ctrls,
  241. .nctrls = ARRAY_SIZE(sd_ctrls),
  242. .config = sd_config,
  243. .init = sd_init,
  244. .isoc_init = sd_isoc_init,
  245. .start = sd_start,
  246. .stopN = sd_stopN,
  247. .pkt_scan = sd_pkt_scan,
  248. };
  249. /* -- module initialisation -- */
  250. static const struct usb_device_id device_table[] = {
  251. {USB_DEVICE(0x04a5, 0x3035)},
  252. {}
  253. };
  254. MODULE_DEVICE_TABLE(usb, device_table);
  255. /* -- device connect -- */
  256. static int sd_probe(struct usb_interface *intf,
  257. const struct usb_device_id *id)
  258. {
  259. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  260. THIS_MODULE);
  261. }
  262. static struct usb_driver sd_driver = {
  263. .name = MODULE_NAME,
  264. .id_table = device_table,
  265. .probe = sd_probe,
  266. .disconnect = gspca_disconnect,
  267. #ifdef CONFIG_PM
  268. .suspend = gspca_suspend,
  269. .resume = gspca_resume,
  270. #endif
  271. };
  272. /* -- module insert / remove -- */
  273. static int __init sd_mod_init(void)
  274. {
  275. return usb_register(&sd_driver);
  276. }
  277. static void __exit sd_mod_exit(void)
  278. {
  279. usb_deregister(&sd_driver);
  280. }
  281. module_init(sd_mod_init);
  282. module_exit(sd_mod_exit);