uss720.c 22 KB

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