uss720.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*****************************************************************************/
  2. /*
  3. * uss720.c -- USS720 USB Parport Cable.
  4. *
  5. * Copyright (C) 1999, 2005, 2010
  6. * Thomas Sailer (t.sailer@alumni.ethz.ch)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * Based on parport_pc.c
  23. *
  24. * History:
  25. * 0.1 04.08.1999 Created
  26. * 0.2 07.08.1999 Some fixes mainly suggested by Tim Waugh
  27. * Interrupt handling currently disabled because
  28. * usb_request_irq crashes somewhere within ohci.c
  29. * for no apparent reason (that is for me, anyway)
  30. * ECP currently untested
  31. * 0.3 10.08.1999 fixing merge errors
  32. * 0.4 13.08.1999 Added Vendor/Product ID of Brad Hard's cable
  33. * 0.5 20.09.1999 usb_control_msg wrapper used
  34. * Nov01.2000 usb_device_table support by Adam J. Richter
  35. * 08.04.2001 Identify version on module load. gb
  36. * 0.6 02.09.2005 Fix "scheduling in interrupt" problem by making save/restore
  37. * context asynchronous
  38. *
  39. */
  40. /*****************************************************************************/
  41. #include <linux/module.h>
  42. #include <linux/socket.h>
  43. #include <linux/parport.h>
  44. #include <linux/init.h>
  45. #include <linux/usb.h>
  46. #include <linux/delay.h>
  47. #include <linux/completion.h>
  48. #include <linux/kref.h>
  49. #include <linux/slab.h>
  50. /*
  51. * Version Information
  52. */
  53. #define DRIVER_VERSION "v0.6"
  54. #define DRIVER_AUTHOR "Thomas M. Sailer, t.sailer@alumni.ethz.ch"
  55. #define DRIVER_DESC "USB Parport Cable driver for Cables using the Lucent Technologies USS720 Chip"
  56. /* --------------------------------------------------------------------- */
  57. struct parport_uss720_private {
  58. struct usb_device *usbdev;
  59. struct parport *pp;
  60. struct kref ref_count;
  61. __u8 reg[7]; /* USB registers */
  62. struct list_head asynclist;
  63. spinlock_t asynclock;
  64. };
  65. struct uss720_async_request {
  66. struct parport_uss720_private *priv;
  67. struct kref ref_count;
  68. struct list_head asynclist;
  69. struct completion compl;
  70. struct urb *urb;
  71. struct usb_ctrlrequest *dr;
  72. __u8 reg[7];
  73. };
  74. /* --------------------------------------------------------------------- */
  75. static void destroy_priv(struct kref *kref)
  76. {
  77. struct parport_uss720_private *priv = container_of(kref, struct parport_uss720_private, ref_count);
  78. dev_dbg(&priv->usbdev->dev, "destroying priv datastructure\n");
  79. usb_put_dev(priv->usbdev);
  80. kfree(priv);
  81. }
  82. static void destroy_async(struct kref *kref)
  83. {
  84. struct uss720_async_request *rq = container_of(kref, struct uss720_async_request, ref_count);
  85. struct parport_uss720_private *priv = rq->priv;
  86. unsigned long flags;
  87. if (likely(rq->urb))
  88. usb_free_urb(rq->urb);
  89. kfree(rq->dr);
  90. spin_lock_irqsave(&priv->asynclock, flags);
  91. list_del_init(&rq->asynclist);
  92. spin_unlock_irqrestore(&priv->asynclock, flags);
  93. kfree(rq);
  94. kref_put(&priv->ref_count, destroy_priv);
  95. }
  96. /* --------------------------------------------------------------------- */
  97. static void async_complete(struct urb *urb)
  98. {
  99. struct uss720_async_request *rq;
  100. struct parport *pp;
  101. struct parport_uss720_private *priv;
  102. int status = urb->status;
  103. rq = urb->context;
  104. priv = rq->priv;
  105. pp = priv->pp;
  106. if (status) {
  107. dev_err(&urb->dev->dev, "async_complete: urb error %d\n",
  108. status);
  109. } else if (rq->dr->bRequest == 3) {
  110. memcpy(priv->reg, rq->reg, sizeof(priv->reg));
  111. #if 0
  112. dev_dbg(&priv->usbdev->dev, "async_complete regs %7ph\n",
  113. priv->reg);
  114. #endif
  115. /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
  116. if (rq->reg[2] & rq->reg[1] & 0x10 && pp)
  117. parport_generic_irq(pp);
  118. }
  119. complete(&rq->compl);
  120. kref_put(&rq->ref_count, destroy_async);
  121. }
  122. static struct uss720_async_request *submit_async_request(struct parport_uss720_private *priv,
  123. __u8 request, __u8 requesttype, __u16 value, __u16 index,
  124. gfp_t mem_flags)
  125. {
  126. struct usb_device *usbdev;
  127. struct uss720_async_request *rq;
  128. unsigned long flags;
  129. int ret;
  130. if (!priv)
  131. return NULL;
  132. usbdev = priv->usbdev;
  133. if (!usbdev)
  134. return NULL;
  135. rq = kzalloc(sizeof(struct uss720_async_request), mem_flags);
  136. if (!rq) {
  137. dev_err(&usbdev->dev, "submit_async_request out of memory\n");
  138. return NULL;
  139. }
  140. kref_init(&rq->ref_count);
  141. INIT_LIST_HEAD(&rq->asynclist);
  142. init_completion(&rq->compl);
  143. kref_get(&priv->ref_count);
  144. rq->priv = priv;
  145. rq->urb = usb_alloc_urb(0, mem_flags);
  146. if (!rq->urb) {
  147. kref_put(&rq->ref_count, destroy_async);
  148. dev_err(&usbdev->dev, "submit_async_request out of memory\n");
  149. return NULL;
  150. }
  151. rq->dr = kmalloc(sizeof(*rq->dr), mem_flags);
  152. if (!rq->dr) {
  153. kref_put(&rq->ref_count, destroy_async);
  154. return NULL;
  155. }
  156. rq->dr->bRequestType = requesttype;
  157. rq->dr->bRequest = request;
  158. rq->dr->wValue = cpu_to_le16(value);
  159. rq->dr->wIndex = cpu_to_le16(index);
  160. rq->dr->wLength = cpu_to_le16((request == 3) ? sizeof(rq->reg) : 0);
  161. usb_fill_control_urb(rq->urb, usbdev, (requesttype & 0x80) ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0),
  162. (unsigned char *)rq->dr,
  163. (request == 3) ? rq->reg : NULL, (request == 3) ? sizeof(rq->reg) : 0, async_complete, rq);
  164. /* rq->urb->transfer_flags |= URB_ASYNC_UNLINK; */
  165. spin_lock_irqsave(&priv->asynclock, flags);
  166. list_add_tail(&rq->asynclist, &priv->asynclist);
  167. spin_unlock_irqrestore(&priv->asynclock, flags);
  168. kref_get(&rq->ref_count);
  169. ret = usb_submit_urb(rq->urb, mem_flags);
  170. if (!ret)
  171. return rq;
  172. destroy_async(&rq->ref_count);
  173. dev_err(&usbdev->dev, "submit_async_request submit_urb failed with %d\n", ret);
  174. return NULL;
  175. }
  176. static unsigned int kill_all_async_requests_priv(struct parport_uss720_private *priv)
  177. {
  178. struct uss720_async_request *rq;
  179. unsigned long flags;
  180. unsigned int ret = 0;
  181. spin_lock_irqsave(&priv->asynclock, flags);
  182. list_for_each_entry(rq, &priv->asynclist, asynclist) {
  183. usb_unlink_urb(rq->urb);
  184. ret++;
  185. }
  186. spin_unlock_irqrestore(&priv->asynclock, flags);
  187. return ret;
  188. }
  189. /* --------------------------------------------------------------------- */
  190. static int get_1284_register(struct parport *pp, unsigned char reg, unsigned char *val, gfp_t mem_flags)
  191. {
  192. struct parport_uss720_private *priv;
  193. struct uss720_async_request *rq;
  194. static const unsigned char regindex[9] = {
  195. 4, 0, 1, 5, 5, 0, 2, 3, 6
  196. };
  197. int ret;
  198. if (!pp)
  199. return -EIO;
  200. priv = pp->private_data;
  201. rq = submit_async_request(priv, 3, 0xc0, ((unsigned int)reg) << 8, 0, mem_flags);
  202. if (!rq) {
  203. dev_err(&priv->usbdev->dev, "get_1284_register(%u) failed",
  204. (unsigned int)reg);
  205. return -EIO;
  206. }
  207. if (!val) {
  208. kref_put(&rq->ref_count, destroy_async);
  209. return 0;
  210. }
  211. if (wait_for_completion_timeout(&rq->compl, HZ)) {
  212. ret = rq->urb->status;
  213. *val = priv->reg[(reg >= 9) ? 0 : regindex[reg]];
  214. if (ret)
  215. printk(KERN_WARNING "get_1284_register: "
  216. "usb error %d\n", ret);
  217. kref_put(&rq->ref_count, destroy_async);
  218. return ret;
  219. }
  220. printk(KERN_WARNING "get_1284_register timeout\n");
  221. kill_all_async_requests_priv(priv);
  222. return -EIO;
  223. }
  224. static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags)
  225. {
  226. struct parport_uss720_private *priv;
  227. struct uss720_async_request *rq;
  228. if (!pp)
  229. return -EIO;
  230. priv = pp->private_data;
  231. rq = submit_async_request(priv, 4, 0x40, (((unsigned int)reg) << 8) | val, 0, mem_flags);
  232. if (!rq) {
  233. dev_err(&priv->usbdev->dev, "set_1284_register(%u,%u) failed",
  234. (unsigned int)reg, (unsigned int)val);
  235. return -EIO;
  236. }
  237. kref_put(&rq->ref_count, destroy_async);
  238. return 0;
  239. }
  240. /* --------------------------------------------------------------------- */
  241. /* ECR modes */
  242. #define ECR_SPP 00
  243. #define ECR_PS2 01
  244. #define ECR_PPF 02
  245. #define ECR_ECP 03
  246. #define ECR_EPP 04
  247. /* Safely change the mode bits in the ECR */
  248. static int change_mode(struct parport *pp, int m)
  249. {
  250. struct parport_uss720_private *priv = pp->private_data;
  251. int mode;
  252. __u8 reg;
  253. if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
  254. return -EIO;
  255. /* Bits <7:5> contain the mode. */
  256. mode = (priv->reg[2] >> 5) & 0x7;
  257. if (mode == m)
  258. return 0;
  259. /* We have to go through mode 000 or 001 */
  260. if (mode > ECR_PS2 && m > ECR_PS2)
  261. if (change_mode(pp, ECR_PS2))
  262. return -EIO;
  263. if (m <= ECR_PS2 && !(priv->reg[1] & 0x20)) {
  264. /* This mode resets the FIFO, so we may
  265. * have to wait for it to drain first. */
  266. unsigned long expire = jiffies + pp->physport->cad->timeout;
  267. switch (mode) {
  268. case ECR_PPF: /* Parallel Port FIFO mode */
  269. case ECR_ECP: /* ECP Parallel Port mode */
  270. /* Poll slowly. */
  271. for (;;) {
  272. if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
  273. return -EIO;
  274. if (priv->reg[2] & 0x01)
  275. break;
  276. if (time_after_eq (jiffies, expire))
  277. /* The FIFO is stuck. */
  278. return -EBUSY;
  279. msleep_interruptible(10);
  280. if (signal_pending (current))
  281. break;
  282. }
  283. }
  284. }
  285. /* Set the mode. */
  286. if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
  287. return -EIO;
  288. if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
  289. return -EIO;
  290. return 0;
  291. }
  292. /*
  293. * Clear TIMEOUT BIT in EPP MODE
  294. */
  295. static int clear_epp_timeout(struct parport *pp)
  296. {
  297. unsigned char stat;
  298. if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
  299. return 1;
  300. return stat & 1;
  301. }
  302. /*
  303. * Access functions.
  304. */
  305. #if 0
  306. static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
  307. {
  308. struct parport *pp = (struct parport *)dev_id;
  309. struct parport_uss720_private *priv = pp->private_data;
  310. if (usbstatus != 0 || len < 4 || !buffer)
  311. return 1;
  312. memcpy(priv->reg, buffer, 4);
  313. /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
  314. if (priv->reg[2] & priv->reg[1] & 0x10)
  315. parport_generic_irq(pp);
  316. return 1;
  317. }
  318. #endif
  319. static void parport_uss720_write_data(struct parport *pp, unsigned char d)
  320. {
  321. set_1284_register(pp, 0, d, GFP_KERNEL);
  322. }
  323. static unsigned char parport_uss720_read_data(struct parport *pp)
  324. {
  325. unsigned char ret;
  326. if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
  327. return 0;
  328. return ret;
  329. }
  330. static void parport_uss720_write_control(struct parport *pp, unsigned char d)
  331. {
  332. struct parport_uss720_private *priv = pp->private_data;
  333. d = (d & 0xf) | (priv->reg[1] & 0xf0);
  334. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  335. return;
  336. priv->reg[1] = d;
  337. }
  338. static unsigned char parport_uss720_read_control(struct parport *pp)
  339. {
  340. struct parport_uss720_private *priv = pp->private_data;
  341. return priv->reg[1] & 0xf; /* Use soft copy */
  342. }
  343. static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
  344. {
  345. struct parport_uss720_private *priv = pp->private_data;
  346. unsigned char d;
  347. mask &= 0x0f;
  348. val &= 0x0f;
  349. d = (priv->reg[1] & (~mask)) ^ val;
  350. if (set_1284_register(pp, 2, d, GFP_ATOMIC))
  351. return 0;
  352. priv->reg[1] = d;
  353. return d & 0xf;
  354. }
  355. static unsigned char parport_uss720_read_status(struct parport *pp)
  356. {
  357. unsigned char ret;
  358. if (get_1284_register(pp, 1, &ret, GFP_ATOMIC))
  359. return 0;
  360. return ret & 0xf8;
  361. }
  362. static void parport_uss720_disable_irq(struct parport *pp)
  363. {
  364. struct parport_uss720_private *priv = pp->private_data;
  365. unsigned char d;
  366. d = priv->reg[1] & ~0x10;
  367. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  368. return;
  369. priv->reg[1] = d;
  370. }
  371. static void parport_uss720_enable_irq(struct parport *pp)
  372. {
  373. struct parport_uss720_private *priv = pp->private_data;
  374. unsigned char d;
  375. d = priv->reg[1] | 0x10;
  376. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  377. return;
  378. priv->reg[1] = d;
  379. }
  380. static void parport_uss720_data_forward (struct parport *pp)
  381. {
  382. struct parport_uss720_private *priv = pp->private_data;
  383. unsigned char d;
  384. d = priv->reg[1] & ~0x20;
  385. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  386. return;
  387. priv->reg[1] = d;
  388. }
  389. static void parport_uss720_data_reverse (struct parport *pp)
  390. {
  391. struct parport_uss720_private *priv = pp->private_data;
  392. unsigned char d;
  393. d = priv->reg[1] | 0x20;
  394. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  395. return;
  396. priv->reg[1] = d;
  397. }
  398. static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
  399. {
  400. s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
  401. s->u.pc.ecr = 0x24;
  402. }
  403. static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
  404. {
  405. struct parport_uss720_private *priv = pp->private_data;
  406. #if 0
  407. if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
  408. return;
  409. #endif
  410. s->u.pc.ctr = priv->reg[1];
  411. s->u.pc.ecr = priv->reg[2];
  412. }
  413. static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
  414. {
  415. struct parport_uss720_private *priv = pp->private_data;
  416. set_1284_register(pp, 2, s->u.pc.ctr, GFP_ATOMIC);
  417. set_1284_register(pp, 6, s->u.pc.ecr, GFP_ATOMIC);
  418. get_1284_register(pp, 2, NULL, GFP_ATOMIC);
  419. priv->reg[1] = s->u.pc.ctr;
  420. priv->reg[2] = s->u.pc.ecr;
  421. }
  422. static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
  423. {
  424. struct parport_uss720_private *priv = pp->private_data;
  425. size_t got = 0;
  426. if (change_mode(pp, ECR_EPP))
  427. return 0;
  428. for (; got < length; got++) {
  429. if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
  430. break;
  431. buf++;
  432. if (priv->reg[0] & 0x01) {
  433. clear_epp_timeout(pp);
  434. break;
  435. }
  436. }
  437. change_mode(pp, ECR_PS2);
  438. return got;
  439. }
  440. static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
  441. {
  442. #if 0
  443. struct parport_uss720_private *priv = pp->private_data;
  444. size_t written = 0;
  445. if (change_mode(pp, ECR_EPP))
  446. return 0;
  447. for (; written < length; written++) {
  448. if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
  449. break;
  450. ((char*)buf)++;
  451. if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
  452. break;
  453. if (priv->reg[0] & 0x01) {
  454. clear_epp_timeout(pp);
  455. break;
  456. }
  457. }
  458. change_mode(pp, ECR_PS2);
  459. return written;
  460. #else
  461. struct parport_uss720_private *priv = pp->private_data;
  462. struct usb_device *usbdev = priv->usbdev;
  463. int rlen;
  464. int i;
  465. if (!usbdev)
  466. return 0;
  467. if (change_mode(pp, ECR_EPP))
  468. return 0;
  469. i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
  470. if (i)
  471. printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buf, length, rlen);
  472. change_mode(pp, ECR_PS2);
  473. return rlen;
  474. #endif
  475. }
  476. static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
  477. {
  478. struct parport_uss720_private *priv = pp->private_data;
  479. size_t got = 0;
  480. if (change_mode(pp, ECR_EPP))
  481. return 0;
  482. for (; got < length; got++) {
  483. if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
  484. break;
  485. buf++;
  486. if (priv->reg[0] & 0x01) {
  487. clear_epp_timeout(pp);
  488. break;
  489. }
  490. }
  491. change_mode(pp, ECR_PS2);
  492. return got;
  493. }
  494. static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
  495. {
  496. struct parport_uss720_private *priv = pp->private_data;
  497. size_t written = 0;
  498. if (change_mode(pp, ECR_EPP))
  499. return 0;
  500. for (; written < length; written++) {
  501. if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
  502. break;
  503. buf++;
  504. if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
  505. break;
  506. if (priv->reg[0] & 0x01) {
  507. clear_epp_timeout(pp);
  508. break;
  509. }
  510. }
  511. change_mode(pp, ECR_PS2);
  512. return written;
  513. }
  514. static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
  515. {
  516. struct parport_uss720_private *priv = pp->private_data;
  517. struct usb_device *usbdev = priv->usbdev;
  518. int rlen;
  519. int i;
  520. if (!usbdev)
  521. return 0;
  522. if (change_mode(pp, ECR_ECP))
  523. return 0;
  524. i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
  525. if (i)
  526. printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
  527. change_mode(pp, ECR_PS2);
  528. return rlen;
  529. }
  530. static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
  531. {
  532. struct parport_uss720_private *priv = pp->private_data;
  533. struct usb_device *usbdev = priv->usbdev;
  534. int rlen;
  535. int i;
  536. if (!usbdev)
  537. return 0;
  538. if (change_mode(pp, ECR_ECP))
  539. return 0;
  540. i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
  541. if (i)
  542. printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %Zu rlen %u\n", buffer, len, rlen);
  543. change_mode(pp, ECR_PS2);
  544. return rlen;
  545. }
  546. static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
  547. {
  548. size_t written = 0;
  549. if (change_mode(pp, ECR_ECP))
  550. return 0;
  551. for (; written < len; written++) {
  552. if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
  553. break;
  554. buffer++;
  555. }
  556. change_mode(pp, ECR_PS2);
  557. return written;
  558. }
  559. static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
  560. {
  561. struct parport_uss720_private *priv = pp->private_data;
  562. struct usb_device *usbdev = priv->usbdev;
  563. int rlen;
  564. int i;
  565. if (!usbdev)
  566. return 0;
  567. if (change_mode(pp, ECR_PPF))
  568. return 0;
  569. i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
  570. if (i)
  571. printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
  572. change_mode(pp, ECR_PS2);
  573. return rlen;
  574. }
  575. /* --------------------------------------------------------------------- */
  576. static struct parport_operations parport_uss720_ops =
  577. {
  578. .owner = THIS_MODULE,
  579. .write_data = parport_uss720_write_data,
  580. .read_data = parport_uss720_read_data,
  581. .write_control = parport_uss720_write_control,
  582. .read_control = parport_uss720_read_control,
  583. .frob_control = parport_uss720_frob_control,
  584. .read_status = parport_uss720_read_status,
  585. .enable_irq = parport_uss720_enable_irq,
  586. .disable_irq = parport_uss720_disable_irq,
  587. .data_forward = parport_uss720_data_forward,
  588. .data_reverse = parport_uss720_data_reverse,
  589. .init_state = parport_uss720_init_state,
  590. .save_state = parport_uss720_save_state,
  591. .restore_state = parport_uss720_restore_state,
  592. .epp_write_data = parport_uss720_epp_write_data,
  593. .epp_read_data = parport_uss720_epp_read_data,
  594. .epp_write_addr = parport_uss720_epp_write_addr,
  595. .epp_read_addr = parport_uss720_epp_read_addr,
  596. .ecp_write_data = parport_uss720_ecp_write_data,
  597. .ecp_read_data = parport_uss720_ecp_read_data,
  598. .ecp_write_addr = parport_uss720_ecp_write_addr,
  599. .compat_write_data = parport_uss720_write_compat,
  600. .nibble_read_data = parport_ieee1284_read_nibble,
  601. .byte_read_data = parport_ieee1284_read_byte,
  602. };
  603. /* --------------------------------------------------------------------- */
  604. static int uss720_probe(struct usb_interface *intf,
  605. const struct usb_device_id *id)
  606. {
  607. struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf));
  608. struct usb_host_interface *interface;
  609. struct usb_host_endpoint *endpoint;
  610. struct parport_uss720_private *priv;
  611. struct parport *pp;
  612. unsigned char reg;
  613. int i;
  614. dev_dbg(&intf->dev, "probe: vendor id 0x%x, device id 0x%x\n",
  615. le16_to_cpu(usbdev->descriptor.idVendor),
  616. le16_to_cpu(usbdev->descriptor.idProduct));
  617. /* our known interfaces have 3 alternate settings */
  618. if (intf->num_altsetting != 3) {
  619. usb_put_dev(usbdev);
  620. return -ENODEV;
  621. }
  622. i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
  623. dev_dbg(&intf->dev, "set interface result %d\n", i);
  624. interface = intf->cur_altsetting;
  625. if (interface->desc.bNumEndpoints < 3) {
  626. usb_put_dev(usbdev);
  627. return -ENODEV;
  628. }
  629. /*
  630. * Allocate parport interface
  631. */
  632. priv = kzalloc(sizeof(struct parport_uss720_private), GFP_KERNEL);
  633. if (!priv) {
  634. usb_put_dev(usbdev);
  635. return -ENOMEM;
  636. }
  637. priv->pp = NULL;
  638. priv->usbdev = usbdev;
  639. kref_init(&priv->ref_count);
  640. spin_lock_init(&priv->asynclock);
  641. INIT_LIST_HEAD(&priv->asynclist);
  642. pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops);
  643. if (!pp) {
  644. printk(KERN_WARNING "uss720: could not register parport\n");
  645. goto probe_abort;
  646. }
  647. priv->pp = pp;
  648. pp->private_data = priv;
  649. pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
  650. /* set the USS720 control register to manual mode, no ECP compression, enable all ints */
  651. set_1284_register(pp, 7, 0x00, GFP_KERNEL);
  652. set_1284_register(pp, 6, 0x30, GFP_KERNEL); /* PS/2 mode */
  653. set_1284_register(pp, 2, 0x0c, GFP_KERNEL);
  654. /* debugging */
  655. get_1284_register(pp, 0, &reg, GFP_KERNEL);
  656. dev_dbg(&intf->dev, "reg: %7ph\n", priv->reg);
  657. endpoint = &interface->endpoint[2];
  658. dev_dbg(&intf->dev, "epaddr %d interval %d\n",
  659. endpoint->desc.bEndpointAddress, endpoint->desc.bInterval);
  660. parport_announce_port(pp);
  661. usb_set_intfdata(intf, pp);
  662. return 0;
  663. probe_abort:
  664. kill_all_async_requests_priv(priv);
  665. kref_put(&priv->ref_count, destroy_priv);
  666. return -ENODEV;
  667. }
  668. static void uss720_disconnect(struct usb_interface *intf)
  669. {
  670. struct parport *pp = usb_get_intfdata(intf);
  671. struct parport_uss720_private *priv;
  672. struct usb_device *usbdev;
  673. dev_dbg(&intf->dev, "disconnect\n");
  674. usb_set_intfdata(intf, NULL);
  675. if (pp) {
  676. priv = pp->private_data;
  677. usbdev = priv->usbdev;
  678. priv->usbdev = NULL;
  679. priv->pp = NULL;
  680. dev_dbg(&intf->dev, "parport_remove_port\n");
  681. parport_remove_port(pp);
  682. parport_put_port(pp);
  683. kill_all_async_requests_priv(priv);
  684. kref_put(&priv->ref_count, destroy_priv);
  685. }
  686. dev_dbg(&intf->dev, "disconnect done\n");
  687. }
  688. /* table of cables that work through this driver */
  689. static const struct usb_device_id uss720_table[] = {
  690. { USB_DEVICE(0x047e, 0x1001) },
  691. { USB_DEVICE(0x0557, 0x2001) },
  692. { USB_DEVICE(0x0729, 0x1284) },
  693. { USB_DEVICE(0x1293, 0x0002) },
  694. { USB_DEVICE(0x050d, 0x0002) },
  695. { } /* Terminating entry */
  696. };
  697. MODULE_DEVICE_TABLE (usb, uss720_table);
  698. static struct usb_driver uss720_driver = {
  699. .name = "uss720",
  700. .probe = uss720_probe,
  701. .disconnect = uss720_disconnect,
  702. .id_table = uss720_table,
  703. };
  704. /* --------------------------------------------------------------------- */
  705. MODULE_AUTHOR(DRIVER_AUTHOR);
  706. MODULE_DESCRIPTION(DRIVER_DESC);
  707. MODULE_LICENSE("GPL");
  708. static int __init uss720_init(void)
  709. {
  710. int retval;
  711. retval = usb_register(&uss720_driver);
  712. if (retval)
  713. goto out;
  714. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  715. DRIVER_DESC "\n");
  716. printk(KERN_INFO KBUILD_MODNAME ": NOTE: this is a special purpose "
  717. "driver to allow nonstandard\n");
  718. printk(KERN_INFO KBUILD_MODNAME ": protocols (eg. bitbang) over "
  719. "USS720 usb to parallel cables\n");
  720. printk(KERN_INFO KBUILD_MODNAME ": If you just want to connect to a "
  721. "printer, use usblp instead\n");
  722. out:
  723. return retval;
  724. }
  725. static void __exit uss720_cleanup(void)
  726. {
  727. usb_deregister(&uss720_driver);
  728. }
  729. module_init(uss720_init);
  730. module_exit(uss720_cleanup);
  731. /* --------------------------------------------------------------------- */