uss720.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*****************************************************************************/
  2. /*
  3. * uss720.c -- USS720 USB Parport Cable.
  4. *
  5. * Copyright (C) 1999, 2005, 2010
  6. * Thomas Sailer (t.sailer@alumni.ethz.ch)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * Based on parport_pc.c
  23. *
  24. * History:
  25. * 0.1 04.08.1999 Created
  26. * 0.2 07.08.1999 Some fixes mainly suggested by Tim Waugh
  27. * Interrupt handling currently disabled because
  28. * usb_request_irq crashes somewhere within ohci.c
  29. * for no apparent reason (that is for me, anyway)
  30. * ECP currently untested
  31. * 0.3 10.08.1999 fixing merge errors
  32. * 0.4 13.08.1999 Added Vendor/Product ID of Brad Hard's cable
  33. * 0.5 20.09.1999 usb_control_msg wrapper used
  34. * Nov01.2000 usb_device_table support by Adam J. Richter
  35. * 08.04.2001 Identify version on module load. gb
  36. * 0.6 02.09.2005 Fix "scheduling in interrupt" problem by making save/restore
  37. * context asynchronous
  38. *
  39. */
  40. /*****************************************************************************/
  41. #include <linux/module.h>
  42. #include <linux/socket.h>
  43. #include <linux/parport.h>
  44. #include <linux/init.h>
  45. #include <linux/usb.h>
  46. #include <linux/delay.h>
  47. #include <linux/completion.h>
  48. #include <linux/kref.h>
  49. #include <linux/slab.h>
  50. /*
  51. * Version Information
  52. */
  53. #define DRIVER_VERSION "v0.6"
  54. #define DRIVER_AUTHOR "Thomas M. Sailer, t.sailer@alumni.ethz.ch"
  55. #define DRIVER_DESC "USB Parport Cable driver for Cables using the Lucent Technologies USS720 Chip"
  56. /* --------------------------------------------------------------------- */
  57. struct parport_uss720_private {
  58. struct usb_device *usbdev;
  59. struct parport *pp;
  60. struct kref ref_count;
  61. __u8 reg[7]; /* USB registers */
  62. struct list_head asynclist;
  63. spinlock_t asynclock;
  64. };
  65. struct uss720_async_request {
  66. struct parport_uss720_private *priv;
  67. struct kref ref_count;
  68. struct list_head asynclist;
  69. struct completion compl;
  70. struct urb *urb;
  71. struct usb_ctrlrequest *dr;
  72. __u8 reg[7];
  73. };
  74. /* --------------------------------------------------------------------- */
  75. static void destroy_priv(struct kref *kref)
  76. {
  77. struct parport_uss720_private *priv = container_of(kref, struct parport_uss720_private, ref_count);
  78. dev_dbg(&priv->usbdev->dev, "destroying priv datastructure\n");
  79. usb_put_dev(priv->usbdev);
  80. kfree(priv);
  81. }
  82. static void destroy_async(struct kref *kref)
  83. {
  84. struct uss720_async_request *rq = container_of(kref, struct uss720_async_request, ref_count);
  85. struct parport_uss720_private *priv = rq->priv;
  86. unsigned long flags;
  87. if (likely(rq->urb))
  88. usb_free_urb(rq->urb);
  89. kfree(rq->dr);
  90. spin_lock_irqsave(&priv->asynclock, flags);
  91. list_del_init(&rq->asynclist);
  92. spin_unlock_irqrestore(&priv->asynclock, flags);
  93. kfree(rq);
  94. kref_put(&priv->ref_count, destroy_priv);
  95. }
  96. /* --------------------------------------------------------------------- */
  97. static void async_complete(struct urb *urb)
  98. {
  99. struct uss720_async_request *rq;
  100. struct parport *pp;
  101. struct parport_uss720_private *priv;
  102. int status = urb->status;
  103. rq = urb->context;
  104. priv = rq->priv;
  105. pp = priv->pp;
  106. if (status) {
  107. dev_err(&urb->dev->dev, "async_complete: urb error %d\n",
  108. status);
  109. } else if (rq->dr->bRequest == 3) {
  110. memcpy(priv->reg, rq->reg, sizeof(priv->reg));
  111. #if 0
  112. dev_dbg(&priv->usbdev->dev, "async_complete regs %7ph\n",
  113. priv->reg);
  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 = kzalloc(sizeof(struct uss720_async_request), mem_flags);
  136. if (!rq)
  137. return NULL;
  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. return NULL;
  147. }
  148. rq->dr = kmalloc(sizeof(*rq->dr), mem_flags);
  149. if (!rq->dr) {
  150. kref_put(&rq->ref_count, destroy_async);
  151. return NULL;
  152. }
  153. rq->dr->bRequestType = requesttype;
  154. rq->dr->bRequest = request;
  155. rq->dr->wValue = cpu_to_le16(value);
  156. rq->dr->wIndex = cpu_to_le16(index);
  157. rq->dr->wLength = cpu_to_le16((request == 3) ? sizeof(rq->reg) : 0);
  158. usb_fill_control_urb(rq->urb, usbdev, (requesttype & 0x80) ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0),
  159. (unsigned char *)rq->dr,
  160. (request == 3) ? rq->reg : NULL, (request == 3) ? sizeof(rq->reg) : 0, async_complete, rq);
  161. /* rq->urb->transfer_flags |= URB_ASYNC_UNLINK; */
  162. spin_lock_irqsave(&priv->asynclock, flags);
  163. list_add_tail(&rq->asynclist, &priv->asynclist);
  164. spin_unlock_irqrestore(&priv->asynclock, flags);
  165. kref_get(&rq->ref_count);
  166. ret = usb_submit_urb(rq->urb, mem_flags);
  167. if (!ret)
  168. return rq;
  169. destroy_async(&rq->ref_count);
  170. dev_err(&usbdev->dev, "submit_async_request submit_urb failed with %d\n", ret);
  171. return NULL;
  172. }
  173. static unsigned int kill_all_async_requests_priv(struct parport_uss720_private *priv)
  174. {
  175. struct uss720_async_request *rq;
  176. unsigned long flags;
  177. unsigned int ret = 0;
  178. spin_lock_irqsave(&priv->asynclock, flags);
  179. list_for_each_entry(rq, &priv->asynclist, asynclist) {
  180. usb_unlink_urb(rq->urb);
  181. ret++;
  182. }
  183. spin_unlock_irqrestore(&priv->asynclock, flags);
  184. return ret;
  185. }
  186. /* --------------------------------------------------------------------- */
  187. static int get_1284_register(struct parport *pp, unsigned char reg, unsigned char *val, gfp_t mem_flags)
  188. {
  189. struct parport_uss720_private *priv;
  190. struct uss720_async_request *rq;
  191. static const unsigned char regindex[9] = {
  192. 4, 0, 1, 5, 5, 0, 2, 3, 6
  193. };
  194. int ret;
  195. if (!pp)
  196. return -EIO;
  197. priv = pp->private_data;
  198. rq = submit_async_request(priv, 3, 0xc0, ((unsigned int)reg) << 8, 0, mem_flags);
  199. if (!rq) {
  200. dev_err(&priv->usbdev->dev, "get_1284_register(%u) failed",
  201. (unsigned int)reg);
  202. return -EIO;
  203. }
  204. if (!val) {
  205. kref_put(&rq->ref_count, destroy_async);
  206. return 0;
  207. }
  208. if (wait_for_completion_timeout(&rq->compl, HZ)) {
  209. ret = rq->urb->status;
  210. *val = priv->reg[(reg >= 9) ? 0 : regindex[reg]];
  211. if (ret)
  212. printk(KERN_WARNING "get_1284_register: "
  213. "usb error %d\n", ret);
  214. kref_put(&rq->ref_count, destroy_async);
  215. return ret;
  216. }
  217. printk(KERN_WARNING "get_1284_register timeout\n");
  218. kill_all_async_requests_priv(priv);
  219. return -EIO;
  220. }
  221. static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags)
  222. {
  223. struct parport_uss720_private *priv;
  224. struct uss720_async_request *rq;
  225. if (!pp)
  226. return -EIO;
  227. priv = pp->private_data;
  228. rq = submit_async_request(priv, 4, 0x40, (((unsigned int)reg) << 8) | val, 0, mem_flags);
  229. if (!rq) {
  230. dev_err(&priv->usbdev->dev, "set_1284_register(%u,%u) failed",
  231. (unsigned int)reg, (unsigned int)val);
  232. return -EIO;
  233. }
  234. kref_put(&rq->ref_count, destroy_async);
  235. return 0;
  236. }
  237. /* --------------------------------------------------------------------- */
  238. /* ECR modes */
  239. #define ECR_SPP 00
  240. #define ECR_PS2 01
  241. #define ECR_PPF 02
  242. #define ECR_ECP 03
  243. #define ECR_EPP 04
  244. /* Safely change the mode bits in the ECR */
  245. static int change_mode(struct parport *pp, int m)
  246. {
  247. struct parport_uss720_private *priv = pp->private_data;
  248. int mode;
  249. __u8 reg;
  250. if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
  251. return -EIO;
  252. /* Bits <7:5> contain the mode. */
  253. mode = (priv->reg[2] >> 5) & 0x7;
  254. if (mode == m)
  255. return 0;
  256. /* We have to go through mode 000 or 001 */
  257. if (mode > ECR_PS2 && m > ECR_PS2)
  258. if (change_mode(pp, ECR_PS2))
  259. return -EIO;
  260. if (m <= ECR_PS2 && !(priv->reg[1] & 0x20)) {
  261. /* This mode resets the FIFO, so we may
  262. * have to wait for it to drain first. */
  263. unsigned long expire = jiffies + pp->physport->cad->timeout;
  264. switch (mode) {
  265. case ECR_PPF: /* Parallel Port FIFO mode */
  266. case ECR_ECP: /* ECP Parallel Port mode */
  267. /* Poll slowly. */
  268. for (;;) {
  269. if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
  270. return -EIO;
  271. if (priv->reg[2] & 0x01)
  272. break;
  273. if (time_after_eq (jiffies, expire))
  274. /* The FIFO is stuck. */
  275. return -EBUSY;
  276. msleep_interruptible(10);
  277. if (signal_pending (current))
  278. break;
  279. }
  280. }
  281. }
  282. /* Set the mode. */
  283. if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
  284. return -EIO;
  285. if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
  286. return -EIO;
  287. return 0;
  288. }
  289. /*
  290. * Clear TIMEOUT BIT in EPP MODE
  291. */
  292. static int clear_epp_timeout(struct parport *pp)
  293. {
  294. unsigned char stat;
  295. if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
  296. return 1;
  297. return stat & 1;
  298. }
  299. /*
  300. * Access functions.
  301. */
  302. #if 0
  303. static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
  304. {
  305. struct parport *pp = (struct parport *)dev_id;
  306. struct parport_uss720_private *priv = pp->private_data;
  307. if (usbstatus != 0 || len < 4 || !buffer)
  308. return 1;
  309. memcpy(priv->reg, buffer, 4);
  310. /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
  311. if (priv->reg[2] & priv->reg[1] & 0x10)
  312. parport_generic_irq(pp);
  313. return 1;
  314. }
  315. #endif
  316. static void parport_uss720_write_data(struct parport *pp, unsigned char d)
  317. {
  318. set_1284_register(pp, 0, d, GFP_KERNEL);
  319. }
  320. static unsigned char parport_uss720_read_data(struct parport *pp)
  321. {
  322. unsigned char ret;
  323. if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
  324. return 0;
  325. return ret;
  326. }
  327. static void parport_uss720_write_control(struct parport *pp, unsigned char d)
  328. {
  329. struct parport_uss720_private *priv = pp->private_data;
  330. d = (d & 0xf) | (priv->reg[1] & 0xf0);
  331. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  332. return;
  333. priv->reg[1] = d;
  334. }
  335. static unsigned char parport_uss720_read_control(struct parport *pp)
  336. {
  337. struct parport_uss720_private *priv = pp->private_data;
  338. return priv->reg[1] & 0xf; /* Use soft copy */
  339. }
  340. static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
  341. {
  342. struct parport_uss720_private *priv = pp->private_data;
  343. unsigned char d;
  344. mask &= 0x0f;
  345. val &= 0x0f;
  346. d = (priv->reg[1] & (~mask)) ^ val;
  347. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  348. return 0;
  349. priv->reg[1] = d;
  350. return d & 0xf;
  351. }
  352. static unsigned char parport_uss720_read_status(struct parport *pp)
  353. {
  354. unsigned char ret;
  355. if (get_1284_register(pp, 1, &ret, GFP_KERNEL))
  356. return 0;
  357. return ret & 0xf8;
  358. }
  359. static void parport_uss720_disable_irq(struct parport *pp)
  360. {
  361. struct parport_uss720_private *priv = pp->private_data;
  362. unsigned char d;
  363. d = priv->reg[1] & ~0x10;
  364. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  365. return;
  366. priv->reg[1] = d;
  367. }
  368. static void parport_uss720_enable_irq(struct parport *pp)
  369. {
  370. struct parport_uss720_private *priv = pp->private_data;
  371. unsigned char d;
  372. d = priv->reg[1] | 0x10;
  373. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  374. return;
  375. priv->reg[1] = d;
  376. }
  377. static void parport_uss720_data_forward (struct parport *pp)
  378. {
  379. struct parport_uss720_private *priv = pp->private_data;
  380. unsigned char d;
  381. d = priv->reg[1] & ~0x20;
  382. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  383. return;
  384. priv->reg[1] = d;
  385. }
  386. static void parport_uss720_data_reverse (struct parport *pp)
  387. {
  388. struct parport_uss720_private *priv = pp->private_data;
  389. unsigned char d;
  390. d = priv->reg[1] | 0x20;
  391. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  392. return;
  393. priv->reg[1] = d;
  394. }
  395. static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
  396. {
  397. s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
  398. s->u.pc.ecr = 0x24;
  399. }
  400. static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
  401. {
  402. struct parport_uss720_private *priv = pp->private_data;
  403. #if 0
  404. if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
  405. return;
  406. #endif
  407. s->u.pc.ctr = priv->reg[1];
  408. s->u.pc.ecr = priv->reg[2];
  409. }
  410. static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
  411. {
  412. struct parport_uss720_private *priv = pp->private_data;
  413. set_1284_register(pp, 2, s->u.pc.ctr, GFP_ATOMIC);
  414. set_1284_register(pp, 6, s->u.pc.ecr, GFP_ATOMIC);
  415. get_1284_register(pp, 2, NULL, GFP_ATOMIC);
  416. priv->reg[1] = s->u.pc.ctr;
  417. priv->reg[2] = s->u.pc.ecr;
  418. }
  419. static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
  420. {
  421. struct parport_uss720_private *priv = pp->private_data;
  422. size_t got = 0;
  423. if (change_mode(pp, ECR_EPP))
  424. return 0;
  425. for (; got < length; got++) {
  426. if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
  427. break;
  428. buf++;
  429. if (priv->reg[0] & 0x01) {
  430. clear_epp_timeout(pp);
  431. break;
  432. }
  433. }
  434. change_mode(pp, ECR_PS2);
  435. return got;
  436. }
  437. static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
  438. {
  439. #if 0
  440. struct parport_uss720_private *priv = pp->private_data;
  441. size_t written = 0;
  442. if (change_mode(pp, ECR_EPP))
  443. return 0;
  444. for (; written < length; written++) {
  445. if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
  446. break;
  447. ((char*)buf)++;
  448. if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
  449. break;
  450. if (priv->reg[0] & 0x01) {
  451. clear_epp_timeout(pp);
  452. break;
  453. }
  454. }
  455. change_mode(pp, ECR_PS2);
  456. return written;
  457. #else
  458. struct parport_uss720_private *priv = pp->private_data;
  459. struct usb_device *usbdev = priv->usbdev;
  460. int rlen;
  461. int i;
  462. if (!usbdev)
  463. return 0;
  464. if (change_mode(pp, ECR_EPP))
  465. return 0;
  466. i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
  467. if (i)
  468. printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buf, length, rlen);
  469. change_mode(pp, ECR_PS2);
  470. return rlen;
  471. #endif
  472. }
  473. static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
  474. {
  475. struct parport_uss720_private *priv = pp->private_data;
  476. size_t got = 0;
  477. if (change_mode(pp, ECR_EPP))
  478. return 0;
  479. for (; got < length; got++) {
  480. if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
  481. break;
  482. buf++;
  483. if (priv->reg[0] & 0x01) {
  484. clear_epp_timeout(pp);
  485. break;
  486. }
  487. }
  488. change_mode(pp, ECR_PS2);
  489. return got;
  490. }
  491. static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
  492. {
  493. struct parport_uss720_private *priv = pp->private_data;
  494. size_t written = 0;
  495. if (change_mode(pp, ECR_EPP))
  496. return 0;
  497. for (; written < length; written++) {
  498. if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
  499. break;
  500. buf++;
  501. if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
  502. break;
  503. if (priv->reg[0] & 0x01) {
  504. clear_epp_timeout(pp);
  505. break;
  506. }
  507. }
  508. change_mode(pp, ECR_PS2);
  509. return written;
  510. }
  511. static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
  512. {
  513. struct parport_uss720_private *priv = pp->private_data;
  514. struct usb_device *usbdev = priv->usbdev;
  515. int rlen;
  516. int i;
  517. if (!usbdev)
  518. return 0;
  519. if (change_mode(pp, ECR_ECP))
  520. return 0;
  521. i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
  522. if (i)
  523. printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
  524. change_mode(pp, ECR_PS2);
  525. return rlen;
  526. }
  527. static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
  528. {
  529. struct parport_uss720_private *priv = pp->private_data;
  530. struct usb_device *usbdev = priv->usbdev;
  531. int rlen;
  532. int i;
  533. if (!usbdev)
  534. return 0;
  535. if (change_mode(pp, ECR_ECP))
  536. return 0;
  537. i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
  538. if (i)
  539. printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %Zu rlen %u\n", buffer, len, rlen);
  540. change_mode(pp, ECR_PS2);
  541. return rlen;
  542. }
  543. static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
  544. {
  545. size_t written = 0;
  546. if (change_mode(pp, ECR_ECP))
  547. return 0;
  548. for (; written < len; written++) {
  549. if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
  550. break;
  551. buffer++;
  552. }
  553. change_mode(pp, ECR_PS2);
  554. return written;
  555. }
  556. static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
  557. {
  558. struct parport_uss720_private *priv = pp->private_data;
  559. struct usb_device *usbdev = priv->usbdev;
  560. int rlen;
  561. int i;
  562. if (!usbdev)
  563. return 0;
  564. if (change_mode(pp, ECR_PPF))
  565. return 0;
  566. i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
  567. if (i)
  568. printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
  569. change_mode(pp, ECR_PS2);
  570. return rlen;
  571. }
  572. /* --------------------------------------------------------------------- */
  573. static struct parport_operations parport_uss720_ops =
  574. {
  575. .owner = THIS_MODULE,
  576. .write_data = parport_uss720_write_data,
  577. .read_data = parport_uss720_read_data,
  578. .write_control = parport_uss720_write_control,
  579. .read_control = parport_uss720_read_control,
  580. .frob_control = parport_uss720_frob_control,
  581. .read_status = parport_uss720_read_status,
  582. .enable_irq = parport_uss720_enable_irq,
  583. .disable_irq = parport_uss720_disable_irq,
  584. .data_forward = parport_uss720_data_forward,
  585. .data_reverse = parport_uss720_data_reverse,
  586. .init_state = parport_uss720_init_state,
  587. .save_state = parport_uss720_save_state,
  588. .restore_state = parport_uss720_restore_state,
  589. .epp_write_data = parport_uss720_epp_write_data,
  590. .epp_read_data = parport_uss720_epp_read_data,
  591. .epp_write_addr = parport_uss720_epp_write_addr,
  592. .epp_read_addr = parport_uss720_epp_read_addr,
  593. .ecp_write_data = parport_uss720_ecp_write_data,
  594. .ecp_read_data = parport_uss720_ecp_read_data,
  595. .ecp_write_addr = parport_uss720_ecp_write_addr,
  596. .compat_write_data = parport_uss720_write_compat,
  597. .nibble_read_data = parport_ieee1284_read_nibble,
  598. .byte_read_data = parport_ieee1284_read_byte,
  599. };
  600. /* --------------------------------------------------------------------- */
  601. static int uss720_probe(struct usb_interface *intf,
  602. const struct usb_device_id *id)
  603. {
  604. struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf));
  605. struct usb_host_interface *interface;
  606. struct usb_host_endpoint *endpoint;
  607. struct parport_uss720_private *priv;
  608. struct parport *pp;
  609. unsigned char reg;
  610. int i;
  611. dev_dbg(&intf->dev, "probe: vendor id 0x%x, device id 0x%x\n",
  612. le16_to_cpu(usbdev->descriptor.idVendor),
  613. le16_to_cpu(usbdev->descriptor.idProduct));
  614. /* our known interfaces have 3 alternate settings */
  615. if (intf->num_altsetting != 3) {
  616. usb_put_dev(usbdev);
  617. return -ENODEV;
  618. }
  619. i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
  620. dev_dbg(&intf->dev, "set interface result %d\n", i);
  621. interface = intf->cur_altsetting;
  622. if (interface->desc.bNumEndpoints < 3) {
  623. usb_put_dev(usbdev);
  624. return -ENODEV;
  625. }
  626. /*
  627. * Allocate parport interface
  628. */
  629. priv = kzalloc(sizeof(struct parport_uss720_private), GFP_KERNEL);
  630. if (!priv) {
  631. usb_put_dev(usbdev);
  632. return -ENOMEM;
  633. }
  634. priv->pp = NULL;
  635. priv->usbdev = usbdev;
  636. kref_init(&priv->ref_count);
  637. spin_lock_init(&priv->asynclock);
  638. INIT_LIST_HEAD(&priv->asynclist);
  639. pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops);
  640. if (!pp) {
  641. printk(KERN_WARNING "uss720: could not register parport\n");
  642. goto probe_abort;
  643. }
  644. priv->pp = pp;
  645. pp->private_data = priv;
  646. pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
  647. /* set the USS720 control register to manual mode, no ECP compression, enable all ints */
  648. set_1284_register(pp, 7, 0x00, GFP_KERNEL);
  649. set_1284_register(pp, 6, 0x30, GFP_KERNEL); /* PS/2 mode */
  650. set_1284_register(pp, 2, 0x0c, GFP_KERNEL);
  651. /* debugging */
  652. get_1284_register(pp, 0, &reg, GFP_KERNEL);
  653. dev_dbg(&intf->dev, "reg: %7ph\n", priv->reg);
  654. endpoint = &interface->endpoint[2];
  655. dev_dbg(&intf->dev, "epaddr %d interval %d\n",
  656. endpoint->desc.bEndpointAddress, endpoint->desc.bInterval);
  657. parport_announce_port(pp);
  658. usb_set_intfdata(intf, pp);
  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_VERSION ":"
  712. DRIVER_DESC "\n");
  713. printk(KERN_INFO KBUILD_MODNAME ": NOTE: this is a special purpose "
  714. "driver to allow nonstandard\n");
  715. printk(KERN_INFO KBUILD_MODNAME ": protocols (eg. bitbang) over "
  716. "USS720 usb to parallel cables\n");
  717. printk(KERN_INFO KBUILD_MODNAME ": If you just want to connect to a "
  718. "printer, use usblp instead\n");
  719. out:
  720. return retval;
  721. }
  722. static void __exit uss720_cleanup(void)
  723. {
  724. usb_deregister(&uss720_driver);
  725. }
  726. module_init(uss720_init);
  727. module_exit(uss720_cleanup);
  728. /* --------------------------------------------------------------------- */