uss720.c 22 KB

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