Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 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 | |
| 25 | MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>"); |
| 26 | MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver"); |
| 27 | MODULE_LICENSE("GPL"); |
| 28 | |
| 29 | /* specific webcam descriptor */ |
| 30 | struct sd { |
| 31 | struct gspca_dev gspca_dev; /* !! must be the first item */ |
| 32 | }; |
| 33 | |
| 34 | /* V4L2 controls supported by the driver */ |
Marton Nemeth | 7e64dc4 | 2009-12-30 09:12:41 -0300 | [diff] [blame] | 35 | static const struct ctrl sd_ctrls[] = { |
Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static 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 | |
| 45 | static void sd_isoc_irq(struct urb *urb); |
| 46 | |
| 47 | /* -- write a register -- */ |
| 48 | static 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) { |
Jean-François Moine | 8dbf3e7 | 2010-09-25 03:19:33 -0300 | [diff] [blame] | 65 | err("reg_w err %d", ret); |
Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 66 | gspca_dev->usb_err = ret; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* this function is called at probe time */ |
| 71 | static 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 */ |
| 82 | static int sd_init(struct gspca_dev *gspca_dev) |
| 83 | { |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static 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 -- */ |
| 102 | static 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; |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 120 | urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev, |
Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 121 | SD_PKT_SZ * SD_NPKT, |
| 122 | GFP_KERNEL, |
| 123 | &urb->transfer_dma); |
| 124 | |
| 125 | if (urb->transfer_buffer == NULL) { |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 126 | err("usb_alloc_coherent failed"); |
Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 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 | |
| 148 | static 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); |
Jean-François Moine | 780e312 | 2010-10-19 04:29:10 -0300 | [diff] [blame] | 155 | usb_set_interface(gspca_dev->dev, gspca_dev->iface, |
| 156 | gspca_dev->nbalt - 1); |
Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void sd_pkt_scan(struct gspca_dev *gspca_dev, |
| 160 | u8 *data, /* isoc packet */ |
| 161 | int len) /* iso packet length */ |
| 162 | { |
| 163 | /* unused */ |
| 164 | } |
| 165 | |
| 166 | /* reception of an URB */ |
| 167 | static void sd_isoc_irq(struct urb *urb) |
| 168 | { |
| 169 | struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; |
| 170 | struct urb *urb0; |
| 171 | u8 *data; |
| 172 | int i, st; |
| 173 | |
| 174 | PDEBUG(D_PACK, "sd isoc irq"); |
| 175 | if (!gspca_dev->streaming) |
| 176 | return; |
| 177 | if (urb->status != 0) { |
| 178 | if (urb->status == -ESHUTDOWN) |
| 179 | return; /* disconnection */ |
| 180 | #ifdef CONFIG_PM |
| 181 | if (gspca_dev->frozen) |
| 182 | return; |
| 183 | #endif |
Jean-François Moine | 8dbf3e7 | 2010-09-25 03:19:33 -0300 | [diff] [blame] | 184 | err("urb status: %d", urb->status); |
Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
| 188 | /* if this is a control URN (ep 0x83), wait */ |
| 189 | if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2]) |
| 190 | return; |
| 191 | |
| 192 | /* scan both received URBs */ |
| 193 | if (urb == gspca_dev->urb[1]) |
| 194 | urb0 = gspca_dev->urb[0]; |
| 195 | else |
| 196 | urb0 = gspca_dev->urb[2]; |
| 197 | for (i = 0; i < urb->number_of_packets; i++) { |
| 198 | |
| 199 | /* check the packet status and length */ |
| 200 | if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ |
| 201 | || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) { |
| 202 | PDEBUG(D_ERR, "ISOC bad lengths %d / %d", |
| 203 | urb0->iso_frame_desc[i].actual_length, |
| 204 | urb->iso_frame_desc[i].actual_length); |
| 205 | gspca_dev->last_packet_type = DISCARD_PACKET; |
| 206 | continue; |
| 207 | } |
| 208 | st = urb0->iso_frame_desc[i].status; |
| 209 | if (st == 0) |
| 210 | st = urb->iso_frame_desc[i].status; |
| 211 | if (st) { |
Jean-François Moine | 8dbf3e7 | 2010-09-25 03:19:33 -0300 | [diff] [blame] | 212 | err("ISOC data error: [%d] status=%d", |
Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 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) |
Jean-François Moine | 8dbf3e7 | 2010-09-25 03:19:33 -0300 | [diff] [blame] | 259 | err("usb_submit_urb(0) ret %d", st); |
Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 260 | st = usb_submit_urb(urb, GFP_ATOMIC); |
| 261 | if (st < 0) |
Jean-François Moine | 8dbf3e7 | 2010-09-25 03:19:33 -0300 | [diff] [blame] | 262 | err("usb_submit_urb() ret %d", st); |
Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* sub-driver description */ |
| 266 | static 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 -- */ |
Jean-François Moine | 95c967c | 2011-01-13 05:20:29 -0300 | [diff] [blame] | 279 | static const struct usb_device_id device_table[] = { |
Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 280 | {USB_DEVICE(0x04a5, 0x3035)}, |
| 281 | {} |
| 282 | }; |
| 283 | MODULE_DEVICE_TABLE(usb, device_table); |
| 284 | |
| 285 | /* -- device connect -- */ |
| 286 | static 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 | |
| 293 | static 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 -- */ |
| 305 | static int __init sd_mod_init(void) |
| 306 | { |
Jean-François Moine | 8129888 | 2010-09-25 03:20:45 -0300 | [diff] [blame] | 307 | return usb_register(&sd_driver); |
Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 308 | } |
| 309 | static void __exit sd_mod_exit(void) |
| 310 | { |
| 311 | usb_deregister(&sd_driver); |
Jean-Francois Moine | 0a71d9c | 2009-12-20 09:09:22 -0300 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | module_init(sd_mod_init); |
| 315 | module_exit(sd_mod_exit); |