blob: a62865af53ccca76e9c123a2075d40d8c7961ce9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * copyright (C) 1999/2000 by Henning Zabel <henning@uni-paderborn.de>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19
20/*
21 * USB-Kernel Driver for the Mustek MDC800 Digital Camera
22 * (c) 1999/2000 Henning Zabel <henning@uni-paderborn.de>
23 *
24 *
25 * The driver brings the USB functions of the MDC800 to Linux.
Steven Cole093cf722005-05-03 19:07:24 -060026 * To use the Camera you must support the USB Protocol of the camera
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * to the Kernel Node.
28 * The Driver uses a misc device Node. Create it with :
29 * mknod /dev/mustek c 180 32
30 *
31 * The driver supports only one camera.
32 *
33 * Fix: mdc800 used sleep_on and slept with io_lock held.
34 * Converted sleep_on to waitqueues with schedule_timeout and made io_lock
35 * a semaphore from a spinlock.
36 * by Oliver Neukum <oliver@neukum.name>
37 * (02/12/2001)
38 *
39 * Identify version on module load.
40 * (08/04/2001) gb
41 *
42 * version 0.7.5
43 * Fixed potential SMP races with Spinlocks.
44 * Thanks to Oliver Neukum <oliver@neukum.name> who
45 * noticed the race conditions.
46 * (30/10/2000)
47 *
48 * Fixed: Setting urb->dev before submitting urb.
49 * by Greg KH <greg@kroah.com>
50 * (13/10/2000)
51 *
52 * version 0.7.3
53 * bugfix : The mdc800->state field gets set to READY after the
Rahul Bedarkar35698432014-01-08 22:02:44 +053054 * the disconnect function sets it to NOT_CONNECTED. This makes the
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * driver running like the camera is connected and causes some
56 * hang ups.
57 *
58 * version 0.7.1
59 * MOD_INC and MOD_DEC are changed in usb_probe to prevent load/unload
60 * problems when compiled as Module.
61 * (04/04/2000)
62 *
63 * The mdc800 driver gets assigned the USB Minor 32-47. The Registration
64 * was updated to use these values.
65 * (26/03/2000)
66 *
67 * The Init und Exit Module Function are updated.
68 * (01/03/2000)
69 *
70 * version 0.7.0
71 * Rewrite of the driver : The driver now uses URB's. The old stuff
72 * has been removed.
73 *
74 * version 0.6.0
75 * Rewrite of this driver: The Emulation of the rs232 protocoll
76 * has been removed from the driver. A special executeCommand function
77 * for this driver is included to gphoto.
78 * The driver supports two kind of communication to bulk endpoints.
79 * Either with the dev->bus->ops->bulk... or with callback function.
80 * (09/11/1999)
81 *
82 * version 0.5.0:
83 * first Version that gets a version number. Most of the needed
84 * functions work.
85 * (20/10/1999)
86 */
87
88#include <linux/sched.h>
89#include <linux/signal.h>
90#include <linux/spinlock.h>
91#include <linux/errno.h>
92#include <linux/random.h>
93#include <linux/poll.h>
94#include <linux/init.h>
95#include <linux/slab.h>
96#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <linux/wait.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010098#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#include <linux/usb.h>
101#include <linux/fs.h>
102
103/*
104 * Version Information
105 */
106#define DRIVER_VERSION "v0.7.5 (30/10/2000)"
107#define DRIVER_AUTHOR "Henning Zabel <henning@uni-paderborn.de>"
108#define DRIVER_DESC "USB Driver for Mustek MDC800 Digital Camera"
109
110/* Vendor and Product Information */
111#define MDC800_VENDOR_ID 0x055f
112#define MDC800_PRODUCT_ID 0xa800
113
114/* Timeouts (msec) */
115#define TO_DOWNLOAD_GET_READY 1500
116#define TO_DOWNLOAD_GET_BUSY 1500
117#define TO_WRITE_GET_READY 1000
118#define TO_DEFAULT_COMMAND 5000
119#define TO_READ_FROM_IRQ TO_DEFAULT_COMMAND
120#define TO_GET_READY TO_DEFAULT_COMMAND
121
122/* Minor Number of the device (create with mknod /dev/mustek c 180 32) */
123#define MDC800_DEVICE_MINOR_BASE 32
124
125
126/**************************************************************************
127 Data and structs
128***************************************************************************/
129
130
131typedef enum {
132 NOT_CONNECTED, READY, WORKING, DOWNLOAD
133} mdc800_state;
134
135
136/* Data for the driver */
137struct mdc800_data
138{
139 struct usb_device * dev; // Device Data
140 mdc800_state state;
141
142 unsigned int endpoint [4];
143
144 struct urb * irq_urb;
145 wait_queue_head_t irq_wait;
146 int irq_woken;
147 char* irq_urb_buffer;
148
149 int camera_busy; // is camera busy ?
150 int camera_request_ready; // Status to synchronize with irq
151 char camera_response [8]; // last Bytes send after busy
152
153 struct urb * write_urb;
154 char* write_urb_buffer;
155 wait_queue_head_t write_wait;
156 int written;
157
158
159 struct urb * download_urb;
160 char* download_urb_buffer;
161 wait_queue_head_t download_wait;
162 int downloaded;
163 int download_left; // Bytes left to download ?
164
165
166 /* Device Data */
167 char out [64]; // Answer Buffer
168 int out_ptr; // Index to the first not readen byte
169 int out_count; // Bytes in the buffer
170
171 int open; // Camera device open ?
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100172 struct mutex io_lock; // IO -lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 char in [8]; // Command Input Buffer
175 int in_count;
176
177 int pic_index; // Cache for the Imagesize (-1 for nothing cached )
178 int pic_len;
179 int minor;
180};
181
182
183/* Specification of the Endpoints */
184static struct usb_endpoint_descriptor mdc800_ed [4] =
185{
186 {
187 .bLength = 0,
188 .bDescriptorType = 0,
189 .bEndpointAddress = 0x01,
190 .bmAttributes = 0x02,
Harvey Harrison551509d2009-02-11 14:11:36 -0800191 .wMaxPacketSize = cpu_to_le16(8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .bInterval = 0,
193 .bRefresh = 0,
194 .bSynchAddress = 0,
195 },
196 {
197 .bLength = 0,
198 .bDescriptorType = 0,
199 .bEndpointAddress = 0x82,
200 .bmAttributes = 0x03,
Harvey Harrison551509d2009-02-11 14:11:36 -0800201 .wMaxPacketSize = cpu_to_le16(8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 .bInterval = 0,
203 .bRefresh = 0,
204 .bSynchAddress = 0,
205 },
206 {
207 .bLength = 0,
208 .bDescriptorType = 0,
209 .bEndpointAddress = 0x03,
210 .bmAttributes = 0x02,
Harvey Harrison551509d2009-02-11 14:11:36 -0800211 .wMaxPacketSize = cpu_to_le16(64),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 .bInterval = 0,
213 .bRefresh = 0,
214 .bSynchAddress = 0,
215 },
216 {
217 .bLength = 0,
218 .bDescriptorType = 0,
219 .bEndpointAddress = 0x84,
220 .bmAttributes = 0x02,
Harvey Harrison551509d2009-02-11 14:11:36 -0800221 .wMaxPacketSize = cpu_to_le16(64),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .bInterval = 0,
223 .bRefresh = 0,
224 .bSynchAddress = 0,
225 },
226};
227
228/* The Variable used by the driver */
229static struct mdc800_data* mdc800;
230
231
232/***************************************************************************
233 The USB Part of the driver
234****************************************************************************/
235
236static int mdc800_endpoint_equals (struct usb_endpoint_descriptor *a,struct usb_endpoint_descriptor *b)
237{
238 return (
239 ( a->bEndpointAddress == b->bEndpointAddress )
240 && ( a->bmAttributes == b->bmAttributes )
241 && ( a->wMaxPacketSize == b->wMaxPacketSize )
242 );
243}
244
245
246/*
247 * Checks whether the camera responds busy
248 */
249static int mdc800_isBusy (char* ch)
250{
251 int i=0;
252 while (i<8)
253 {
254 if (ch [i] != (char)0x99)
255 return 0;
256 i++;
257 }
258 return 1;
259}
260
261
262/*
263 * Checks whether the Camera is ready
264 */
265static int mdc800_isReady (char *ch)
266{
267 int i=0;
268 while (i<8)
269 {
270 if (ch [i] != (char)0xbb)
271 return 0;
272 i++;
273 }
274 return 1;
275}
276
277
278
279/*
280 * USB IRQ Handler for InputLine
281 */
David Howells7d12e782006-10-05 14:55:46 +0100282static void mdc800_usb_irq (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 int data_received=0, wake_up;
285 unsigned char* b=urb->transfer_buffer;
286 struct mdc800_data* mdc800=urb->context;
Greg Kroah-Hartman1b6d8232012-05-01 21:33:50 -0700287 struct device *dev = &mdc800->dev->dev;
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700288 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700290 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (mdc800_isBusy (b))
292 {
293 if (!mdc800->camera_busy)
294 {
295 mdc800->camera_busy=1;
Greg Kroah-Hartman1b6d8232012-05-01 21:33:50 -0700296 dev_dbg(dev, "gets busy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298 }
299 else
300 {
301 if (mdc800->camera_busy && mdc800_isReady (b))
302 {
303 mdc800->camera_busy=0;
Greg Kroah-Hartman1b6d8232012-05-01 21:33:50 -0700304 dev_dbg(dev, "gets ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306 }
307 if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
308 {
309 /* Store Data in camera_answer field */
Greg Kroah-Hartman1b6d8232012-05-01 21:33:50 -0700310 dev_dbg(dev, "%i %i %i %i %i %i %i %i \n",b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 memcpy (mdc800->camera_response,b,8);
313 data_received=1;
314 }
315 }
316 wake_up= ( mdc800->camera_request_ready > 0 )
317 &&
318 (
319 ((mdc800->camera_request_ready == 1) && (!mdc800->camera_busy))
320 ||
321 ((mdc800->camera_request_ready == 2) && data_received)
322 ||
323 ((mdc800->camera_request_ready == 3) && (mdc800->camera_busy))
324 ||
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700325 (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 );
327
328 if (wake_up)
329 {
330 mdc800->camera_request_ready=0;
331 mdc800->irq_woken=1;
332 wake_up (&mdc800->irq_wait);
333 }
334}
335
336
337/*
338 * Waits a while until the irq responds that camera is ready
339 *
340 * mode : 0: Wait for camera gets ready
341 * 1: Wait for receiving data
342 * 2: Wait for camera gets busy
343 *
344 * msec: Time to wait
345 */
346static int mdc800_usb_waitForIRQ (int mode, int msec)
347{
348 mdc800->camera_request_ready=1+mode;
349
350 wait_event_timeout(mdc800->irq_wait, mdc800->irq_woken, msec*HZ/1000);
351 mdc800->irq_woken = 0;
352
353 if (mdc800->camera_request_ready>0)
354 {
355 mdc800->camera_request_ready=0;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700356 dev_err(&mdc800->dev->dev, "timeout waiting for camera.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -1;
358 }
359
360 if (mdc800->state == NOT_CONNECTED)
361 {
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700362 printk(KERN_WARNING "mdc800: Camera gets disconnected "
363 "during waiting for irq.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 mdc800->camera_request_ready=0;
365 return -2;
366 }
367
368 return 0;
369}
370
371
372/*
373 * The write_urb callback function
374 */
David Howells7d12e782006-10-05 14:55:46 +0100375static void mdc800_usb_write_notify (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct mdc800_data* mdc800=urb->context;
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700378 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700380 if (status != 0)
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700381 dev_err(&mdc800->dev->dev,
382 "writing command fails (status=%i)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 mdc800->state=READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 mdc800->written = 1;
386 wake_up (&mdc800->write_wait);
387}
388
389
390/*
391 * The download_urb callback function
392 */
David Howells7d12e782006-10-05 14:55:46 +0100393static void mdc800_usb_download_notify (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct mdc800_data* mdc800=urb->context;
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700396 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700398 if (status == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* Fill output buffer with these data */
400 memcpy (mdc800->out, urb->transfer_buffer, 64);
401 mdc800->out_count=64;
402 mdc800->out_ptr=0;
403 mdc800->download_left-=64;
404 if (mdc800->download_left == 0)
405 {
406 mdc800->state=READY;
407 }
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700408 } else {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700409 dev_err(&mdc800->dev->dev,
410 "request bytes fails (status:%i)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 mdc800->downloaded = 1;
413 wake_up (&mdc800->download_wait);
414}
415
416
417/***************************************************************************
418 Probing for the Camera
419 ***************************************************************************/
420
421static struct usb_driver mdc800_usb_driver;
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300422static const struct file_operations mdc800_device_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static struct usb_class_driver mdc800_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700424 .name = "mdc800%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 .fops = &mdc800_device_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 .minor_base = MDC800_DEVICE_MINOR_BASE,
427};
428
429
430/*
431 * Callback to search the Mustek MDC800 on the USB Bus
432 */
433static int mdc800_usb_probe (struct usb_interface *intf,
434 const struct usb_device_id *id)
435{
436 int i,j;
437 struct usb_host_interface *intf_desc;
438 struct usb_device *dev = interface_to_usbdev (intf);
439 int irq_interval=0;
440 int retval;
441
Greg Kroah-Hartman1b6d8232012-05-01 21:33:50 -0700442 dev_dbg(&intf->dev, "(%s) called.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444
445 if (mdc800->dev != NULL)
446 {
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700447 dev_warn(&intf->dev, "only one Mustek MDC800 is supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return -ENODEV;
449 }
450
451 if (dev->descriptor.bNumConfigurations != 1)
452 {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700453 dev_err(&intf->dev,
454 "probe fails -> wrong Number of Configuration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return -ENODEV;
456 }
457 intf_desc = intf->cur_altsetting;
458
459 if (
460 ( intf_desc->desc.bInterfaceClass != 0xff )
461 || ( intf_desc->desc.bInterfaceSubClass != 0 )
462 || ( intf_desc->desc.bInterfaceProtocol != 0 )
463 || ( intf_desc->desc.bNumEndpoints != 4)
464 )
465 {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700466 dev_err(&intf->dev, "probe fails -> wrong Interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return -ENODEV;
468 }
469
470 /* Check the Endpoints */
471 for (i=0; i<4; i++)
472 {
473 mdc800->endpoint[i]=-1;
474 for (j=0; j<4; j++)
475 {
476 if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
477 {
478 mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
479 if (i==1)
480 {
481 irq_interval=intf_desc->endpoint [j].desc.bInterval;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 }
485 if (mdc800->endpoint[i] == -1)
486 {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700487 dev_err(&intf->dev, "probe fails -> Wrong Endpoints.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return -ENODEV;
489 }
490 }
491
492
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700493 dev_info(&intf->dev, "Found Mustek MDC800 on USB.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100495 mutex_lock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 retval = usb_register_dev(intf, &mdc800_class);
498 if (retval) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700499 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
Jiri Slaby909b6c32009-03-11 21:47:37 +0100500 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -ENODEV;
502 }
503
504 mdc800->dev=dev;
505 mdc800->open=0;
506
507 /* Setup URB Structs */
508 usb_fill_int_urb (
509 mdc800->irq_urb,
510 mdc800->dev,
511 usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
512 mdc800->irq_urb_buffer,
513 8,
514 mdc800_usb_irq,
515 mdc800,
516 irq_interval
517 );
518
519 usb_fill_bulk_urb (
520 mdc800->write_urb,
521 mdc800->dev,
522 usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
523 mdc800->write_urb_buffer,
524 8,
525 mdc800_usb_write_notify,
526 mdc800
527 );
528
529 usb_fill_bulk_urb (
530 mdc800->download_urb,
531 mdc800->dev,
532 usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
533 mdc800->download_urb_buffer,
534 64,
535 mdc800_usb_download_notify,
536 mdc800
537 );
538
539 mdc800->state=READY;
540
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100541 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 usb_set_intfdata(intf, mdc800);
544 return 0;
545}
546
547
548/*
549 * Disconnect USB device (maybe the MDC800)
550 */
551static void mdc800_usb_disconnect (struct usb_interface *intf)
552{
553 struct mdc800_data* mdc800 = usb_get_intfdata(intf);
554
Greg Kroah-Hartman1b6d8232012-05-01 21:33:50 -0700555 dev_dbg(&intf->dev, "(%s) called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 if (mdc800) {
558 if (mdc800->state == NOT_CONNECTED)
559 return;
560
561 usb_deregister_dev(intf, &mdc800_class);
562
Oliver Neukumf38649f2007-01-05 17:42:35 +0100563 /* must be under lock to make sure no URB
564 is submitted after usb_kill_urb() */
565 mutex_lock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 mdc800->state=NOT_CONNECTED;
567
568 usb_kill_urb(mdc800->irq_urb);
569 usb_kill_urb(mdc800->write_urb);
570 usb_kill_urb(mdc800->download_urb);
Oliver Neukumf38649f2007-01-05 17:42:35 +0100571 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 mdc800->dev = NULL;
574 usb_set_intfdata(intf, NULL);
575 }
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700576 dev_info(&intf->dev, "Mustek MDC800 disconnected from USB.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579
580/***************************************************************************
581 The Misc device Part (file_operations)
582****************************************************************************/
583
584/*
585 * This Function calc the Answersize for a command.
586 */
587static int mdc800_getAnswerSize (char command)
588{
589 switch ((unsigned char) command)
590 {
591 case 0x2a:
592 case 0x49:
593 case 0x51:
594 case 0x0d:
595 case 0x20:
596 case 0x07:
597 case 0x01:
598 case 0x25:
599 case 0x00:
600 return 8;
601
602 case 0x05:
603 case 0x3e:
604 return mdc800->pic_len;
605
606 case 0x09:
607 return 4096;
608
609 default:
610 return 0;
611 }
612}
613
614
615/*
616 * Init the device: (1) alloc mem (2) Increase MOD Count ..
617 */
618static int mdc800_device_open (struct inode* inode, struct file *file)
619{
620 int retval=0;
621 int errn=0;
622
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100623 mutex_lock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 if (mdc800->state == NOT_CONNECTED)
626 {
627 errn=-EBUSY;
628 goto error_out;
629 }
630 if (mdc800->open)
631 {
632 errn=-EBUSY;
633 goto error_out;
634 }
635
636 mdc800->in_count=0;
637 mdc800->out_count=0;
638 mdc800->out_ptr=0;
639 mdc800->pic_index=0;
640 mdc800->pic_len=-1;
641 mdc800->download_left=0;
642
643 mdc800->camera_busy=0;
644 mdc800->camera_request_ready=0;
645
646 retval=0;
647 mdc800->irq_urb->dev = mdc800->dev;
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700648 retval = usb_submit_urb (mdc800->irq_urb, GFP_KERNEL);
649 if (retval) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700650 dev_err(&mdc800->dev->dev,
651 "request USB irq fails (submit_retval=%i).\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 errn = -EIO;
653 goto error_out;
654 }
655
656 mdc800->open=1;
Greg Kroah-Hartman1b6d8232012-05-01 21:33:50 -0700657 dev_dbg(&mdc800->dev->dev, "Mustek MDC800 device opened.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659error_out:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100660 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return errn;
662}
663
664
665/*
666 * Close the Camera and release Memory
667 */
668static int mdc800_device_release (struct inode* inode, struct file *file)
669{
670 int retval=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100672 mutex_lock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (mdc800->open && (mdc800->state != NOT_CONNECTED))
674 {
675 usb_kill_urb(mdc800->irq_urb);
676 usb_kill_urb(mdc800->write_urb);
677 usb_kill_urb(mdc800->download_urb);
678 mdc800->open=0;
679 }
680 else
681 {
682 retval=-EIO;
683 }
684
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100685 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return retval;
687}
688
689
690/*
691 * The Device read callback Function
692 */
693static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t len, loff_t *pos)
694{
695 size_t left=len, sts=len; /* single transfer size */
696 char __user *ptr = buf;
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700697 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100699 mutex_lock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (mdc800->state == NOT_CONNECTED)
701 {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100702 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return -EBUSY;
704 }
705 if (mdc800->state == WORKING)
706 {
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700707 printk(KERN_WARNING "mdc800: Illegal State \"working\""
708 "reached during read ?!\n");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100709 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -EBUSY;
711 }
712 if (!mdc800->open)
713 {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100714 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -EBUSY;
716 }
717
718 while (left)
719 {
720 if (signal_pending (current))
721 {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100722 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return -EINTR;
724 }
725
726 sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
727
728 if (sts <= 0)
729 {
730 /* Too less Data in buffer */
731 if (mdc800->state == DOWNLOAD)
732 {
733 mdc800->out_count=0;
734 mdc800->out_ptr=0;
735
736 /* Download -> Request new bytes */
737 mdc800->download_urb->dev = mdc800->dev;
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700738 retval = usb_submit_urb (mdc800->download_urb, GFP_KERNEL);
739 if (retval) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700740 dev_err(&mdc800->dev->dev,
741 "Can't submit download urb "
742 "(retval=%i)\n", retval);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100743 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return len-left;
745 }
746 wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
747 TO_DOWNLOAD_GET_READY*HZ/1000);
748 mdc800->downloaded = 0;
749 if (mdc800->download_urb->status != 0)
750 {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700751 dev_err(&mdc800->dev->dev,
752 "request download-bytes fails "
753 "(status=%i)\n",
754 mdc800->download_urb->status);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100755 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return len-left;
757 }
758 }
759 else
760 {
761 /* No more bytes -> that's an error*/
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100762 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -EIO;
764 }
765 }
766 else
767 {
768 /* Copy Bytes */
769 if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
770 sts)) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100771 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -EFAULT;
773 }
774 ptr+=sts;
775 left-=sts;
776 mdc800->out_ptr+=sts;
777 }
778 }
779
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100780 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return len-left;
782}
783
784
785/*
786 * The Device write callback Function
787 * If a 8Byte Command is received, it will be send to the camera.
788 * After this the driver initiates the request for the answer or
789 * just waits until the camera becomes ready.
790 */
791static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
792{
793 size_t i=0;
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700794 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100796 mutex_lock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (mdc800->state != READY)
798 {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100799 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return -EBUSY;
801 }
802 if (!mdc800->open )
803 {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100804 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return -EBUSY;
806 }
807
808 while (i<len)
809 {
810 unsigned char c;
811 if (signal_pending (current))
812 {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100813 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -EINTR;
815 }
816
817 if(get_user(c, buf+i))
818 {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100819 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return -EFAULT;
821 }
822
823 /* check for command start */
824 if (c == 0x55)
825 {
826 mdc800->in_count=0;
827 mdc800->out_count=0;
828 mdc800->out_ptr=0;
829 mdc800->download_left=0;
830 }
831
832 /* save command byte */
833 if (mdc800->in_count < 8)
834 {
835 mdc800->in[mdc800->in_count] = c;
836 mdc800->in_count++;
837 }
838 else
839 {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100840 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return -EIO;
842 }
843
844 /* Command Buffer full ? -> send it to camera */
845 if (mdc800->in_count == 8)
846 {
847 int answersize;
848
849 if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
850 {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700851 dev_err(&mdc800->dev->dev,
852 "Camera didn't get ready.\n");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100853 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return -EIO;
855 }
856
857 answersize=mdc800_getAnswerSize (mdc800->in[1]);
858
859 mdc800->state=WORKING;
860 memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
861 mdc800->write_urb->dev = mdc800->dev;
Greg Kroah-Hartmandd6fa4d2007-07-18 10:58:02 -0700862 retval = usb_submit_urb (mdc800->write_urb, GFP_KERNEL);
863 if (retval) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700864 dev_err(&mdc800->dev->dev,
865 "submitting write urb fails "
866 "(retval=%i)\n", retval);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100867 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return -EIO;
869 }
870 wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
871 mdc800->written = 0;
872 if (mdc800->state == WORKING)
873 {
874 usb_kill_urb(mdc800->write_urb);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100875 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return -EIO;
877 }
878
879 switch ((unsigned char) mdc800->in[1])
880 {
881 case 0x05: /* Download Image */
882 case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
883 if (mdc800->pic_len < 0)
884 {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700885 dev_err(&mdc800->dev->dev,
886 "call 0x07 before "
887 "0x05,0x3e\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 mdc800->state=READY;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100889 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return -EIO;
891 }
892 mdc800->pic_len=-1;
893
894 case 0x09: /* Download Thumbnail */
895 mdc800->download_left=answersize+64;
896 mdc800->state=DOWNLOAD;
897 mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
898 break;
899
900
901 default:
902 if (answersize)
903 {
904
905 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
906 {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700907 dev_err(&mdc800->dev->dev, "requesting answer from irq fails\n");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100908 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -EIO;
910 }
911
912 /* Write dummy data, (this is ugly but part of the USB Protocol */
913 /* if you use endpoint 1 as bulk and not as irq) */
914 memcpy (mdc800->out, mdc800->camera_response,8);
915
916 /* This is the interpreted answer */
917 memcpy (&mdc800->out[8], mdc800->camera_response,8);
918
919 mdc800->out_ptr=0;
920 mdc800->out_count=16;
921
922 /* Cache the Imagesize, if command was getImageSize */
923 if (mdc800->in [1] == (char) 0x07)
924 {
925 mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
926
Greg Kroah-Hartman1b6d8232012-05-01 21:33:50 -0700927 dev_dbg(&mdc800->dev->dev, "cached imagesize = %i\n", mdc800->pic_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
930 }
931 else
932 {
933 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
934 {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700935 dev_err(&mdc800->dev->dev, "Command Timeout.\n");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100936 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return -EIO;
938 }
939 }
940 mdc800->state=READY;
941 break;
942 }
943 }
944 i++;
945 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100946 mutex_unlock(&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return i;
948}
949
950
951/***************************************************************************
952 Init and Cleanup this driver (Structs and types)
953****************************************************************************/
954
955/* File Operations of this drivers */
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300956static const struct file_operations mdc800_device_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 .owner = THIS_MODULE,
959 .read = mdc800_device_read,
960 .write = mdc800_device_write,
961 .open = mdc800_device_open,
962 .release = mdc800_device_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200963 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964};
965
966
967
Németh Márton90521272010-01-10 15:34:36 +0100968static const struct usb_device_id mdc800_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
970 { } /* Terminating entry */
971};
972
973MODULE_DEVICE_TABLE (usb, mdc800_table);
974/*
975 * USB Driver Struct for this device
976 */
977static struct usb_driver mdc800_usb_driver =
978{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 .name = "mdc800",
980 .probe = mdc800_usb_probe,
981 .disconnect = mdc800_usb_disconnect,
982 .id_table = mdc800_table
983};
984
985
986
987/************************************************************************
988 Init and Cleanup this driver (Main Functions)
989*************************************************************************/
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991static int __init usb_mdc800_init (void)
992{
993 int retval = -ENODEV;
994 /* Allocate Memory */
Oliver Neukum9ff87d72006-01-06 20:45:11 +0100995 mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
Alexey Dobriyan72129cd2005-10-25 23:34:03 +0400996 if (!mdc800)
997 goto cleanup_on_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 mdc800->dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 mdc800->state=NOT_CONNECTED;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001001 mutex_init (&mdc800->io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 init_waitqueue_head (&mdc800->irq_wait);
1004 init_waitqueue_head (&mdc800->write_wait);
1005 init_waitqueue_head (&mdc800->download_wait);
1006
1007 mdc800->irq_woken = 0;
1008 mdc800->downloaded = 0;
1009 mdc800->written = 0;
1010
Alexey Dobriyan72129cd2005-10-25 23:34:03 +04001011 mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL);
1012 if (!mdc800->irq_urb_buffer)
1013 goto cleanup_on_fail;
1014 mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL);
1015 if (!mdc800->write_urb_buffer)
1016 goto cleanup_on_fail;
1017 mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL);
1018 if (!mdc800->download_urb_buffer)
1019 goto cleanup_on_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Alexey Dobriyan72129cd2005-10-25 23:34:03 +04001021 mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL);
1022 if (!mdc800->irq_urb)
1023 goto cleanup_on_fail;
1024 mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL);
1025 if (!mdc800->download_urb)
1026 goto cleanup_on_fail;
1027 mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL);
1028 if (!mdc800->write_urb)
1029 goto cleanup_on_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 /* Register the driver */
1032 retval = usb_register(&mdc800_usb_driver);
1033 if (retval)
1034 goto cleanup_on_fail;
1035
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -07001036 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1037 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 return 0;
1040
1041 /* Clean driver up, when something fails */
1042
1043cleanup_on_fail:
1044
1045 if (mdc800 != NULL)
1046 {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001047 printk(KERN_ERR "mdc800: can't alloc memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 kfree(mdc800->download_urb_buffer);
1050 kfree(mdc800->write_urb_buffer);
1051 kfree(mdc800->irq_urb_buffer);
1052
1053 usb_free_urb(mdc800->write_urb);
1054 usb_free_urb(mdc800->download_urb);
1055 usb_free_urb(mdc800->irq_urb);
1056
1057 kfree (mdc800);
1058 }
1059 mdc800 = NULL;
1060 return retval;
1061}
1062
1063
1064static void __exit usb_mdc800_cleanup (void)
1065{
1066 usb_deregister (&mdc800_usb_driver);
1067
1068 usb_free_urb (mdc800->irq_urb);
1069 usb_free_urb (mdc800->download_urb);
1070 usb_free_urb (mdc800->write_urb);
1071
1072 kfree (mdc800->irq_urb_buffer);
1073 kfree (mdc800->write_urb_buffer);
1074 kfree (mdc800->download_urb_buffer);
1075
1076 kfree (mdc800);
1077 mdc800 = NULL;
1078}
1079
1080module_init (usb_mdc800_init);
1081module_exit (usb_mdc800_cleanup);
1082
1083MODULE_AUTHOR( DRIVER_AUTHOR );
1084MODULE_DESCRIPTION( DRIVER_DESC );
1085MODULE_LICENSE("GPL");
1086