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