uss720.c 22 KB

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