mdc800.c 24 KB

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