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