mdc800.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /*
  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. * USB-Kernel Driver for the Mustek MDC800 Digital Camera
  20. * (c) 1999/2000 Henning Zabel <henning@uni-paderborn.de>
  21. *
  22. *
  23. * The driver brings the USB functions of the MDC800 to Linux.
  24. * To use the Camera you must support the USB Protocol of the camera
  25. * to the Kernel Node.
  26. * The Driver uses a misc device Node. Create it with :
  27. * mknod /dev/mustek c 180 32
  28. *
  29. * The driver supports only one camera.
  30. *
  31. * Fix: mdc800 used sleep_on and slept with io_lock held.
  32. * Converted sleep_on to waitqueues with schedule_timeout and made io_lock
  33. * a semaphore from a spinlock.
  34. * by Oliver Neukum <oliver@neukum.name>
  35. * (02/12/2001)
  36. *
  37. * Identify version on module load.
  38. * (08/04/2001) gb
  39. *
  40. * version 0.7.5
  41. * Fixed potential SMP races with Spinlocks.
  42. * Thanks to Oliver Neukum <oliver@neukum.name> who
  43. * noticed the race conditions.
  44. * (30/10/2000)
  45. *
  46. * Fixed: Setting urb->dev before submitting urb.
  47. * by Greg KH <greg@kroah.com>
  48. * (13/10/2000)
  49. *
  50. * version 0.7.3
  51. * bugfix : The mdc800->state field gets set to READY after the
  52. * the diconnect function sets it to NOT_CONNECTED. This makes the
  53. * driver running like the camera is connected and causes some
  54. * hang ups.
  55. *
  56. * version 0.7.1
  57. * MOD_INC and MOD_DEC are changed in usb_probe to prevent load/unload
  58. * problems when compiled as Module.
  59. * (04/04/2000)
  60. *
  61. * The mdc800 driver gets assigned the USB Minor 32-47. The Registration
  62. * was updated to use these values.
  63. * (26/03/2000)
  64. *
  65. * The Init und Exit Module Function are updated.
  66. * (01/03/2000)
  67. *
  68. * version 0.7.0
  69. * Rewrite of the driver : The driver now uses URB's. The old stuff
  70. * has been removed.
  71. *
  72. * version 0.6.0
  73. * Rewrite of this driver: The Emulation of the rs232 protocoll
  74. * has been removed from the driver. A special executeCommand function
  75. * for this driver is included to gphoto.
  76. * The driver supports two kind of communication to bulk endpoints.
  77. * Either with the dev->bus->ops->bulk... or with callback function.
  78. * (09/11/1999)
  79. *
  80. * version 0.5.0:
  81. * first Version that gets a version number. Most of the needed
  82. * functions work.
  83. * (20/10/1999)
  84. */
  85. #include <linux/sched.h>
  86. #include <linux/signal.h>
  87. #include <linux/spinlock.h>
  88. #include <linux/errno.h>
  89. #include <linux/random.h>
  90. #include <linux/poll.h>
  91. #include <linux/init.h>
  92. #include <linux/slab.h>
  93. #include <linux/module.h>
  94. #include <linux/smp_lock.h>
  95. #include <linux/wait.h>
  96. #include <linux/mutex.h>
  97. #include <linux/usb.h>
  98. #include <linux/fs.h>
  99. /*
  100. * Version Information
  101. */
  102. #define DRIVER_VERSION "v0.7.5 (30/10/2000)"
  103. #define DRIVER_AUTHOR "Henning Zabel <henning@uni-paderborn.de>"
  104. #define DRIVER_DESC "USB Driver for Mustek MDC800 Digital Camera"
  105. /* Vendor and Product Information */
  106. #define MDC800_VENDOR_ID 0x055f
  107. #define MDC800_PRODUCT_ID 0xa800
  108. /* Timeouts (msec) */
  109. #define TO_DOWNLOAD_GET_READY 1500
  110. #define TO_DOWNLOAD_GET_BUSY 1500
  111. #define TO_WRITE_GET_READY 1000
  112. #define TO_DEFAULT_COMMAND 5000
  113. #define TO_READ_FROM_IRQ TO_DEFAULT_COMMAND
  114. #define TO_GET_READY TO_DEFAULT_COMMAND
  115. /* Minor Number of the device (create with mknod /dev/mustek c 180 32) */
  116. #define MDC800_DEVICE_MINOR_BASE 32
  117. /**************************************************************************
  118. Data and structs
  119. ***************************************************************************/
  120. typedef enum {
  121. NOT_CONNECTED, READY, WORKING, DOWNLOAD
  122. } mdc800_state;
  123. /* Data for the driver */
  124. struct mdc800_data
  125. {
  126. struct usb_device * dev; // Device Data
  127. mdc800_state state;
  128. unsigned int endpoint [4];
  129. struct urb * irq_urb;
  130. wait_queue_head_t irq_wait;
  131. int irq_woken;
  132. char* irq_urb_buffer;
  133. int camera_busy; // is camera busy ?
  134. int camera_request_ready; // Status to synchronize with irq
  135. char camera_response [8]; // last Bytes send after busy
  136. struct urb * write_urb;
  137. char* write_urb_buffer;
  138. wait_queue_head_t write_wait;
  139. int written;
  140. struct urb * download_urb;
  141. char* download_urb_buffer;
  142. wait_queue_head_t download_wait;
  143. int downloaded;
  144. int download_left; // Bytes left to download ?
  145. /* Device Data */
  146. char out [64]; // Answer Buffer
  147. int out_ptr; // Index to the first not readen byte
  148. int out_count; // Bytes in the buffer
  149. int open; // Camera device open ?
  150. struct mutex io_lock; // IO -lock
  151. char in [8]; // Command Input Buffer
  152. int in_count;
  153. int pic_index; // Cache for the Imagesize (-1 for nothing cached )
  154. int pic_len;
  155. int minor;
  156. };
  157. /* Specification of the Endpoints */
  158. static struct usb_endpoint_descriptor mdc800_ed [4] =
  159. {
  160. {
  161. .bLength = 0,
  162. .bDescriptorType = 0,
  163. .bEndpointAddress = 0x01,
  164. .bmAttributes = 0x02,
  165. .wMaxPacketSize = __constant_cpu_to_le16(8),
  166. .bInterval = 0,
  167. .bRefresh = 0,
  168. .bSynchAddress = 0,
  169. },
  170. {
  171. .bLength = 0,
  172. .bDescriptorType = 0,
  173. .bEndpointAddress = 0x82,
  174. .bmAttributes = 0x03,
  175. .wMaxPacketSize = __constant_cpu_to_le16(8),
  176. .bInterval = 0,
  177. .bRefresh = 0,
  178. .bSynchAddress = 0,
  179. },
  180. {
  181. .bLength = 0,
  182. .bDescriptorType = 0,
  183. .bEndpointAddress = 0x03,
  184. .bmAttributes = 0x02,
  185. .wMaxPacketSize = __constant_cpu_to_le16(64),
  186. .bInterval = 0,
  187. .bRefresh = 0,
  188. .bSynchAddress = 0,
  189. },
  190. {
  191. .bLength = 0,
  192. .bDescriptorType = 0,
  193. .bEndpointAddress = 0x84,
  194. .bmAttributes = 0x02,
  195. .wMaxPacketSize = __constant_cpu_to_le16(64),
  196. .bInterval = 0,
  197. .bRefresh = 0,
  198. .bSynchAddress = 0,
  199. },
  200. };
  201. /* The Variable used by the driver */
  202. static struct mdc800_data* mdc800;
  203. /***************************************************************************
  204. The USB Part of the driver
  205. ****************************************************************************/
  206. static int mdc800_endpoint_equals (struct usb_endpoint_descriptor *a,struct usb_endpoint_descriptor *b)
  207. {
  208. return (
  209. ( a->bEndpointAddress == b->bEndpointAddress )
  210. && ( a->bmAttributes == b->bmAttributes )
  211. && ( a->wMaxPacketSize == b->wMaxPacketSize )
  212. );
  213. }
  214. /*
  215. * Checks whether the camera responds busy
  216. */
  217. static int mdc800_isBusy (char* ch)
  218. {
  219. int i=0;
  220. while (i<8)
  221. {
  222. if (ch [i] != (char)0x99)
  223. return 0;
  224. i++;
  225. }
  226. return 1;
  227. }
  228. /*
  229. * Checks whether the Camera is ready
  230. */
  231. static int mdc800_isReady (char *ch)
  232. {
  233. int i=0;
  234. while (i<8)
  235. {
  236. if (ch [i] != (char)0xbb)
  237. return 0;
  238. i++;
  239. }
  240. return 1;
  241. }
  242. /*
  243. * USB IRQ Handler for InputLine
  244. */
  245. static void mdc800_usb_irq (struct urb *urb)
  246. {
  247. int data_received=0, wake_up;
  248. unsigned char* b=urb->transfer_buffer;
  249. struct mdc800_data* mdc800=urb->context;
  250. if (urb->status >= 0)
  251. {
  252. //dbg ("%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]);
  253. if (mdc800_isBusy (b))
  254. {
  255. if (!mdc800->camera_busy)
  256. {
  257. mdc800->camera_busy=1;
  258. dbg ("gets busy");
  259. }
  260. }
  261. else
  262. {
  263. if (mdc800->camera_busy && mdc800_isReady (b))
  264. {
  265. mdc800->camera_busy=0;
  266. dbg ("gets ready");
  267. }
  268. }
  269. if (!(mdc800_isBusy (b) || mdc800_isReady (b)))
  270. {
  271. /* Store Data in camera_answer field */
  272. dbg ("%i %i %i %i %i %i %i %i ",b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7]);
  273. memcpy (mdc800->camera_response,b,8);
  274. data_received=1;
  275. }
  276. }
  277. wake_up= ( mdc800->camera_request_ready > 0 )
  278. &&
  279. (
  280. ((mdc800->camera_request_ready == 1) && (!mdc800->camera_busy))
  281. ||
  282. ((mdc800->camera_request_ready == 2) && data_received)
  283. ||
  284. ((mdc800->camera_request_ready == 3) && (mdc800->camera_busy))
  285. ||
  286. (urb->status < 0)
  287. );
  288. if (wake_up)
  289. {
  290. mdc800->camera_request_ready=0;
  291. mdc800->irq_woken=1;
  292. wake_up (&mdc800->irq_wait);
  293. }
  294. }
  295. /*
  296. * Waits a while until the irq responds that camera is ready
  297. *
  298. * mode : 0: Wait for camera gets ready
  299. * 1: Wait for receiving data
  300. * 2: Wait for camera gets busy
  301. *
  302. * msec: Time to wait
  303. */
  304. static int mdc800_usb_waitForIRQ (int mode, int msec)
  305. {
  306. mdc800->camera_request_ready=1+mode;
  307. wait_event_timeout(mdc800->irq_wait, mdc800->irq_woken, msec*HZ/1000);
  308. mdc800->irq_woken = 0;
  309. if (mdc800->camera_request_ready>0)
  310. {
  311. mdc800->camera_request_ready=0;
  312. err ("timeout waiting for camera.");
  313. return -1;
  314. }
  315. if (mdc800->state == NOT_CONNECTED)
  316. {
  317. warn ("Camera gets disconnected during waiting for irq.");
  318. mdc800->camera_request_ready=0;
  319. return -2;
  320. }
  321. return 0;
  322. }
  323. /*
  324. * The write_urb callback function
  325. */
  326. static void mdc800_usb_write_notify (struct urb *urb)
  327. {
  328. struct mdc800_data* mdc800=urb->context;
  329. if (urb->status != 0)
  330. {
  331. err ("writing command fails (status=%i)", urb->status);
  332. }
  333. else
  334. {
  335. mdc800->state=READY;
  336. }
  337. mdc800->written = 1;
  338. wake_up (&mdc800->write_wait);
  339. }
  340. /*
  341. * The download_urb callback function
  342. */
  343. static void mdc800_usb_download_notify (struct urb *urb)
  344. {
  345. struct mdc800_data* mdc800=urb->context;
  346. if (urb->status == 0)
  347. {
  348. /* Fill output buffer with these data */
  349. memcpy (mdc800->out, urb->transfer_buffer, 64);
  350. mdc800->out_count=64;
  351. mdc800->out_ptr=0;
  352. mdc800->download_left-=64;
  353. if (mdc800->download_left == 0)
  354. {
  355. mdc800->state=READY;
  356. }
  357. }
  358. else
  359. {
  360. err ("request bytes fails (status:%i)", urb->status);
  361. }
  362. mdc800->downloaded = 1;
  363. wake_up (&mdc800->download_wait);
  364. }
  365. /***************************************************************************
  366. Probing for the Camera
  367. ***************************************************************************/
  368. static struct usb_driver mdc800_usb_driver;
  369. static const struct file_operations mdc800_device_ops;
  370. static struct usb_class_driver mdc800_class = {
  371. .name = "mdc800%d",
  372. .fops = &mdc800_device_ops,
  373. .minor_base = MDC800_DEVICE_MINOR_BASE,
  374. };
  375. /*
  376. * Callback to search the Mustek MDC800 on the USB Bus
  377. */
  378. static int mdc800_usb_probe (struct usb_interface *intf,
  379. const struct usb_device_id *id)
  380. {
  381. int i,j;
  382. struct usb_host_interface *intf_desc;
  383. struct usb_device *dev = interface_to_usbdev (intf);
  384. int irq_interval=0;
  385. int retval;
  386. dbg ("(mdc800_usb_probe) called.");
  387. if (mdc800->dev != NULL)
  388. {
  389. warn ("only one Mustek MDC800 is supported.");
  390. return -ENODEV;
  391. }
  392. if (dev->descriptor.bNumConfigurations != 1)
  393. {
  394. err ("probe fails -> wrong Number of Configuration");
  395. return -ENODEV;
  396. }
  397. intf_desc = intf->cur_altsetting;
  398. if (
  399. ( intf_desc->desc.bInterfaceClass != 0xff )
  400. || ( intf_desc->desc.bInterfaceSubClass != 0 )
  401. || ( intf_desc->desc.bInterfaceProtocol != 0 )
  402. || ( intf_desc->desc.bNumEndpoints != 4)
  403. )
  404. {
  405. err ("probe fails -> wrong Interface");
  406. return -ENODEV;
  407. }
  408. /* Check the Endpoints */
  409. for (i=0; i<4; i++)
  410. {
  411. mdc800->endpoint[i]=-1;
  412. for (j=0; j<4; j++)
  413. {
  414. if (mdc800_endpoint_equals (&intf_desc->endpoint [j].desc,&mdc800_ed [i]))
  415. {
  416. mdc800->endpoint[i]=intf_desc->endpoint [j].desc.bEndpointAddress ;
  417. if (i==1)
  418. {
  419. irq_interval=intf_desc->endpoint [j].desc.bInterval;
  420. }
  421. continue;
  422. }
  423. }
  424. if (mdc800->endpoint[i] == -1)
  425. {
  426. err ("probe fails -> Wrong Endpoints.");
  427. return -ENODEV;
  428. }
  429. }
  430. info ("Found Mustek MDC800 on USB.");
  431. mutex_lock(&mdc800->io_lock);
  432. retval = usb_register_dev(intf, &mdc800_class);
  433. if (retval) {
  434. err ("Not able to get a minor for this device.");
  435. return -ENODEV;
  436. }
  437. mdc800->dev=dev;
  438. mdc800->open=0;
  439. /* Setup URB Structs */
  440. usb_fill_int_urb (
  441. mdc800->irq_urb,
  442. mdc800->dev,
  443. usb_rcvintpipe (mdc800->dev,mdc800->endpoint [1]),
  444. mdc800->irq_urb_buffer,
  445. 8,
  446. mdc800_usb_irq,
  447. mdc800,
  448. irq_interval
  449. );
  450. usb_fill_bulk_urb (
  451. mdc800->write_urb,
  452. mdc800->dev,
  453. usb_sndbulkpipe (mdc800->dev, mdc800->endpoint[0]),
  454. mdc800->write_urb_buffer,
  455. 8,
  456. mdc800_usb_write_notify,
  457. mdc800
  458. );
  459. usb_fill_bulk_urb (
  460. mdc800->download_urb,
  461. mdc800->dev,
  462. usb_rcvbulkpipe (mdc800->dev, mdc800->endpoint [3]),
  463. mdc800->download_urb_buffer,
  464. 64,
  465. mdc800_usb_download_notify,
  466. mdc800
  467. );
  468. mdc800->state=READY;
  469. mutex_unlock(&mdc800->io_lock);
  470. usb_set_intfdata(intf, mdc800);
  471. return 0;
  472. }
  473. /*
  474. * Disconnect USB device (maybe the MDC800)
  475. */
  476. static void mdc800_usb_disconnect (struct usb_interface *intf)
  477. {
  478. struct mdc800_data* mdc800 = usb_get_intfdata(intf);
  479. dbg ("(mdc800_usb_disconnect) called");
  480. if (mdc800) {
  481. if (mdc800->state == NOT_CONNECTED)
  482. return;
  483. usb_deregister_dev(intf, &mdc800_class);
  484. /* must be under lock to make sure no URB
  485. is submitted after usb_kill_urb() */
  486. mutex_lock(&mdc800->io_lock);
  487. mdc800->state=NOT_CONNECTED;
  488. usb_kill_urb(mdc800->irq_urb);
  489. usb_kill_urb(mdc800->write_urb);
  490. usb_kill_urb(mdc800->download_urb);
  491. mutex_unlock(&mdc800->io_lock);
  492. mdc800->dev = NULL;
  493. usb_set_intfdata(intf, NULL);
  494. }
  495. info ("Mustek MDC800 disconnected from USB.");
  496. }
  497. /***************************************************************************
  498. The Misc device Part (file_operations)
  499. ****************************************************************************/
  500. /*
  501. * This Function calc the Answersize for a command.
  502. */
  503. static int mdc800_getAnswerSize (char command)
  504. {
  505. switch ((unsigned char) command)
  506. {
  507. case 0x2a:
  508. case 0x49:
  509. case 0x51:
  510. case 0x0d:
  511. case 0x20:
  512. case 0x07:
  513. case 0x01:
  514. case 0x25:
  515. case 0x00:
  516. return 8;
  517. case 0x05:
  518. case 0x3e:
  519. return mdc800->pic_len;
  520. case 0x09:
  521. return 4096;
  522. default:
  523. return 0;
  524. }
  525. }
  526. /*
  527. * Init the device: (1) alloc mem (2) Increase MOD Count ..
  528. */
  529. static int mdc800_device_open (struct inode* inode, struct file *file)
  530. {
  531. int retval=0;
  532. int errn=0;
  533. mutex_lock(&mdc800->io_lock);
  534. if (mdc800->state == NOT_CONNECTED)
  535. {
  536. errn=-EBUSY;
  537. goto error_out;
  538. }
  539. if (mdc800->open)
  540. {
  541. errn=-EBUSY;
  542. goto error_out;
  543. }
  544. mdc800->in_count=0;
  545. mdc800->out_count=0;
  546. mdc800->out_ptr=0;
  547. mdc800->pic_index=0;
  548. mdc800->pic_len=-1;
  549. mdc800->download_left=0;
  550. mdc800->camera_busy=0;
  551. mdc800->camera_request_ready=0;
  552. retval=0;
  553. mdc800->irq_urb->dev = mdc800->dev;
  554. if (usb_submit_urb (mdc800->irq_urb, GFP_KERNEL))
  555. {
  556. err ("request USB irq fails (submit_retval=%i urb_status=%i).",retval, mdc800->irq_urb->status);
  557. errn = -EIO;
  558. goto error_out;
  559. }
  560. mdc800->open=1;
  561. dbg ("Mustek MDC800 device opened.");
  562. error_out:
  563. mutex_unlock(&mdc800->io_lock);
  564. return errn;
  565. }
  566. /*
  567. * Close the Camera and release Memory
  568. */
  569. static int mdc800_device_release (struct inode* inode, struct file *file)
  570. {
  571. int retval=0;
  572. dbg ("Mustek MDC800 device closed.");
  573. mutex_lock(&mdc800->io_lock);
  574. if (mdc800->open && (mdc800->state != NOT_CONNECTED))
  575. {
  576. usb_kill_urb(mdc800->irq_urb);
  577. usb_kill_urb(mdc800->write_urb);
  578. usb_kill_urb(mdc800->download_urb);
  579. mdc800->open=0;
  580. }
  581. else
  582. {
  583. retval=-EIO;
  584. }
  585. mutex_unlock(&mdc800->io_lock);
  586. return retval;
  587. }
  588. /*
  589. * The Device read callback Function
  590. */
  591. static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t len, loff_t *pos)
  592. {
  593. size_t left=len, sts=len; /* single transfer size */
  594. char __user *ptr = buf;
  595. mutex_lock(&mdc800->io_lock);
  596. if (mdc800->state == NOT_CONNECTED)
  597. {
  598. mutex_unlock(&mdc800->io_lock);
  599. return -EBUSY;
  600. }
  601. if (mdc800->state == WORKING)
  602. {
  603. warn ("Illegal State \"working\" reached during read ?!");
  604. mutex_unlock(&mdc800->io_lock);
  605. return -EBUSY;
  606. }
  607. if (!mdc800->open)
  608. {
  609. mutex_unlock(&mdc800->io_lock);
  610. return -EBUSY;
  611. }
  612. while (left)
  613. {
  614. if (signal_pending (current))
  615. {
  616. mutex_unlock(&mdc800->io_lock);
  617. return -EINTR;
  618. }
  619. sts=left > (mdc800->out_count-mdc800->out_ptr)?mdc800->out_count-mdc800->out_ptr:left;
  620. if (sts <= 0)
  621. {
  622. /* Too less Data in buffer */
  623. if (mdc800->state == DOWNLOAD)
  624. {
  625. mdc800->out_count=0;
  626. mdc800->out_ptr=0;
  627. /* Download -> Request new bytes */
  628. mdc800->download_urb->dev = mdc800->dev;
  629. if (usb_submit_urb (mdc800->download_urb, GFP_KERNEL))
  630. {
  631. err ("Can't submit download urb (status=%i)",mdc800->download_urb->status);
  632. mutex_unlock(&mdc800->io_lock);
  633. return len-left;
  634. }
  635. wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
  636. TO_DOWNLOAD_GET_READY*HZ/1000);
  637. mdc800->downloaded = 0;
  638. if (mdc800->download_urb->status != 0)
  639. {
  640. err ("request download-bytes fails (status=%i)",mdc800->download_urb->status);
  641. mutex_unlock(&mdc800->io_lock);
  642. return len-left;
  643. }
  644. }
  645. else
  646. {
  647. /* No more bytes -> that's an error*/
  648. mutex_unlock(&mdc800->io_lock);
  649. return -EIO;
  650. }
  651. }
  652. else
  653. {
  654. /* Copy Bytes */
  655. if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
  656. sts)) {
  657. mutex_unlock(&mdc800->io_lock);
  658. return -EFAULT;
  659. }
  660. ptr+=sts;
  661. left-=sts;
  662. mdc800->out_ptr+=sts;
  663. }
  664. }
  665. mutex_unlock(&mdc800->io_lock);
  666. return len-left;
  667. }
  668. /*
  669. * The Device write callback Function
  670. * If a 8Byte Command is received, it will be send to the camera.
  671. * After this the driver initiates the request for the answer or
  672. * just waits until the camera becomes ready.
  673. */
  674. static ssize_t mdc800_device_write (struct file *file, const char __user *buf, size_t len, loff_t *pos)
  675. {
  676. size_t i=0;
  677. mutex_lock(&mdc800->io_lock);
  678. if (mdc800->state != READY)
  679. {
  680. mutex_unlock(&mdc800->io_lock);
  681. return -EBUSY;
  682. }
  683. if (!mdc800->open )
  684. {
  685. mutex_unlock(&mdc800->io_lock);
  686. return -EBUSY;
  687. }
  688. while (i<len)
  689. {
  690. unsigned char c;
  691. if (signal_pending (current))
  692. {
  693. mutex_unlock(&mdc800->io_lock);
  694. return -EINTR;
  695. }
  696. if(get_user(c, buf+i))
  697. {
  698. mutex_unlock(&mdc800->io_lock);
  699. return -EFAULT;
  700. }
  701. /* check for command start */
  702. if (c == 0x55)
  703. {
  704. mdc800->in_count=0;
  705. mdc800->out_count=0;
  706. mdc800->out_ptr=0;
  707. mdc800->download_left=0;
  708. }
  709. /* save command byte */
  710. if (mdc800->in_count < 8)
  711. {
  712. mdc800->in[mdc800->in_count] = c;
  713. mdc800->in_count++;
  714. }
  715. else
  716. {
  717. mutex_unlock(&mdc800->io_lock);
  718. return -EIO;
  719. }
  720. /* Command Buffer full ? -> send it to camera */
  721. if (mdc800->in_count == 8)
  722. {
  723. int answersize;
  724. if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
  725. {
  726. err ("Camera didn't get ready.\n");
  727. mutex_unlock(&mdc800->io_lock);
  728. return -EIO;
  729. }
  730. answersize=mdc800_getAnswerSize (mdc800->in[1]);
  731. mdc800->state=WORKING;
  732. memcpy (mdc800->write_urb->transfer_buffer, mdc800->in,8);
  733. mdc800->write_urb->dev = mdc800->dev;
  734. if (usb_submit_urb (mdc800->write_urb, GFP_KERNEL))
  735. {
  736. err ("submitting write urb fails (status=%i)", mdc800->write_urb->status);
  737. mutex_unlock(&mdc800->io_lock);
  738. return -EIO;
  739. }
  740. wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
  741. mdc800->written = 0;
  742. if (mdc800->state == WORKING)
  743. {
  744. usb_kill_urb(mdc800->write_urb);
  745. mutex_unlock(&mdc800->io_lock);
  746. return -EIO;
  747. }
  748. switch ((unsigned char) mdc800->in[1])
  749. {
  750. case 0x05: /* Download Image */
  751. case 0x3e: /* Take shot in Fine Mode (WCam Mode) */
  752. if (mdc800->pic_len < 0)
  753. {
  754. err ("call 0x07 before 0x05,0x3e");
  755. mdc800->state=READY;
  756. mutex_unlock(&mdc800->io_lock);
  757. return -EIO;
  758. }
  759. mdc800->pic_len=-1;
  760. case 0x09: /* Download Thumbnail */
  761. mdc800->download_left=answersize+64;
  762. mdc800->state=DOWNLOAD;
  763. mdc800_usb_waitForIRQ (0,TO_DOWNLOAD_GET_BUSY);
  764. break;
  765. default:
  766. if (answersize)
  767. {
  768. if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
  769. {
  770. err ("requesting answer from irq fails");
  771. mutex_unlock(&mdc800->io_lock);
  772. return -EIO;
  773. }
  774. /* Write dummy data, (this is ugly but part of the USB Protocol */
  775. /* if you use endpoint 1 as bulk and not as irq) */
  776. memcpy (mdc800->out, mdc800->camera_response,8);
  777. /* This is the interpreted answer */
  778. memcpy (&mdc800->out[8], mdc800->camera_response,8);
  779. mdc800->out_ptr=0;
  780. mdc800->out_count=16;
  781. /* Cache the Imagesize, if command was getImageSize */
  782. if (mdc800->in [1] == (char) 0x07)
  783. {
  784. mdc800->pic_len=(int) 65536*(unsigned char) mdc800->camera_response[0]+256*(unsigned char) mdc800->camera_response[1]+(unsigned char) mdc800->camera_response[2];
  785. dbg ("cached imagesize = %i",mdc800->pic_len);
  786. }
  787. }
  788. else
  789. {
  790. if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
  791. {
  792. err ("Command Timeout.");
  793. mutex_unlock(&mdc800->io_lock);
  794. return -EIO;
  795. }
  796. }
  797. mdc800->state=READY;
  798. break;
  799. }
  800. }
  801. i++;
  802. }
  803. mutex_unlock(&mdc800->io_lock);
  804. return i;
  805. }
  806. /***************************************************************************
  807. Init and Cleanup this driver (Structs and types)
  808. ****************************************************************************/
  809. /* File Operations of this drivers */
  810. static const struct file_operations mdc800_device_ops =
  811. {
  812. .owner = THIS_MODULE,
  813. .read = mdc800_device_read,
  814. .write = mdc800_device_write,
  815. .open = mdc800_device_open,
  816. .release = mdc800_device_release,
  817. };
  818. static struct usb_device_id mdc800_table [] = {
  819. { USB_DEVICE(MDC800_VENDOR_ID, MDC800_PRODUCT_ID) },
  820. { } /* Terminating entry */
  821. };
  822. MODULE_DEVICE_TABLE (usb, mdc800_table);
  823. /*
  824. * USB Driver Struct for this device
  825. */
  826. static struct usb_driver mdc800_usb_driver =
  827. {
  828. .name = "mdc800",
  829. .probe = mdc800_usb_probe,
  830. .disconnect = mdc800_usb_disconnect,
  831. .id_table = mdc800_table
  832. };
  833. /************************************************************************
  834. Init and Cleanup this driver (Main Functions)
  835. *************************************************************************/
  836. static int __init usb_mdc800_init (void)
  837. {
  838. int retval = -ENODEV;
  839. /* Allocate Memory */
  840. mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
  841. if (!mdc800)
  842. goto cleanup_on_fail;
  843. mdc800->dev = NULL;
  844. mdc800->state=NOT_CONNECTED;
  845. mutex_init (&mdc800->io_lock);
  846. init_waitqueue_head (&mdc800->irq_wait);
  847. init_waitqueue_head (&mdc800->write_wait);
  848. init_waitqueue_head (&mdc800->download_wait);
  849. mdc800->irq_woken = 0;
  850. mdc800->downloaded = 0;
  851. mdc800->written = 0;
  852. mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL);
  853. if (!mdc800->irq_urb_buffer)
  854. goto cleanup_on_fail;
  855. mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL);
  856. if (!mdc800->write_urb_buffer)
  857. goto cleanup_on_fail;
  858. mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL);
  859. if (!mdc800->download_urb_buffer)
  860. goto cleanup_on_fail;
  861. mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL);
  862. if (!mdc800->irq_urb)
  863. goto cleanup_on_fail;
  864. mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL);
  865. if (!mdc800->download_urb)
  866. goto cleanup_on_fail;
  867. mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL);
  868. if (!mdc800->write_urb)
  869. goto cleanup_on_fail;
  870. /* Register the driver */
  871. retval = usb_register(&mdc800_usb_driver);
  872. if (retval)
  873. goto cleanup_on_fail;
  874. info (DRIVER_VERSION ":" DRIVER_DESC);
  875. return 0;
  876. /* Clean driver up, when something fails */
  877. cleanup_on_fail:
  878. if (mdc800 != NULL)
  879. {
  880. err ("can't alloc memory!");
  881. kfree(mdc800->download_urb_buffer);
  882. kfree(mdc800->write_urb_buffer);
  883. kfree(mdc800->irq_urb_buffer);
  884. usb_free_urb(mdc800->write_urb);
  885. usb_free_urb(mdc800->download_urb);
  886. usb_free_urb(mdc800->irq_urb);
  887. kfree (mdc800);
  888. }
  889. mdc800 = NULL;
  890. return retval;
  891. }
  892. static void __exit usb_mdc800_cleanup (void)
  893. {
  894. usb_deregister (&mdc800_usb_driver);
  895. usb_free_urb (mdc800->irq_urb);
  896. usb_free_urb (mdc800->download_urb);
  897. usb_free_urb (mdc800->write_urb);
  898. kfree (mdc800->irq_urb_buffer);
  899. kfree (mdc800->write_urb_buffer);
  900. kfree (mdc800->download_urb_buffer);
  901. kfree (mdc800);
  902. mdc800 = NULL;
  903. }
  904. module_init (usb_mdc800_init);
  905. module_exit (usb_mdc800_cleanup);
  906. MODULE_AUTHOR( DRIVER_AUTHOR );
  907. MODULE_DESCRIPTION( DRIVER_DESC );
  908. MODULE_LICENSE("GPL");