uss720.c 22 KB

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