uss720.c 23 KB

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