ser-gigaset.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /* This is the serial hardware link layer (HLL) for the Gigaset 307x isdn
  2. * DECT base (aka Sinus 45 isdn) using the RS232 DECT data module M101,
  3. * written as a line discipline.
  4. *
  5. * =====================================================================
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. * =====================================================================
  11. */
  12. #include "gigaset.h"
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/completion.h>
  17. /* Version Information */
  18. #define DRIVER_AUTHOR "Tilman Schmidt"
  19. #define DRIVER_DESC "Serial Driver for Gigaset 307x using Siemens M101"
  20. #define GIGASET_MINORS 1
  21. #define GIGASET_MINOR 0
  22. #define GIGASET_MODULENAME "ser_gigaset"
  23. #define GIGASET_DEVNAME "ttyGS"
  24. /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
  25. #define IF_WRITEBUF 264
  26. MODULE_AUTHOR(DRIVER_AUTHOR);
  27. MODULE_DESCRIPTION(DRIVER_DESC);
  28. MODULE_LICENSE("GPL");
  29. MODULE_ALIAS_LDISC(N_GIGASET_M101);
  30. static int startmode = SM_ISDN;
  31. module_param(startmode, int, S_IRUGO);
  32. MODULE_PARM_DESC(startmode, "initial operation mode");
  33. static int cidmode = 1;
  34. module_param(cidmode, int, S_IRUGO);
  35. MODULE_PARM_DESC(cidmode, "stay in CID mode when idle");
  36. static struct gigaset_driver *driver;
  37. struct ser_cardstate {
  38. struct platform_device dev;
  39. struct tty_struct *tty;
  40. atomic_t refcnt;
  41. struct completion dead_cmp;
  42. };
  43. static struct platform_driver device_driver = {
  44. .driver = {
  45. .name = GIGASET_MODULENAME,
  46. },
  47. };
  48. static void flush_send_queue(struct cardstate *);
  49. /* transmit data from current open skb
  50. * result: number of bytes sent or error code < 0
  51. */
  52. static int write_modem(struct cardstate *cs)
  53. {
  54. struct tty_struct *tty = cs->hw.ser->tty;
  55. struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
  56. struct sk_buff *skb = bcs->tx_skb;
  57. int sent = -EOPNOTSUPP;
  58. if (!tty || !tty->driver || !skb)
  59. return -EINVAL;
  60. if (!skb->len) {
  61. dev_kfree_skb_any(skb);
  62. bcs->tx_skb = NULL;
  63. return -EINVAL;
  64. }
  65. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  66. if (tty->ops->write)
  67. sent = tty->ops->write(tty, skb->data, skb->len);
  68. gig_dbg(DEBUG_OUTPUT, "write_modem: sent %d", sent);
  69. if (sent < 0) {
  70. /* error */
  71. flush_send_queue(cs);
  72. return sent;
  73. }
  74. skb_pull(skb, sent);
  75. if (!skb->len) {
  76. /* skb sent completely */
  77. gigaset_skb_sent(bcs, skb);
  78. gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
  79. (unsigned long) skb);
  80. dev_kfree_skb_any(skb);
  81. bcs->tx_skb = NULL;
  82. }
  83. return sent;
  84. }
  85. /*
  86. * transmit first queued command buffer
  87. * result: number of bytes sent or error code < 0
  88. */
  89. static int send_cb(struct cardstate *cs)
  90. {
  91. struct tty_struct *tty = cs->hw.ser->tty;
  92. struct cmdbuf_t *cb, *tcb;
  93. unsigned long flags;
  94. int sent = 0;
  95. if (!tty || !tty->driver)
  96. return -EFAULT;
  97. cb = cs->cmdbuf;
  98. if (!cb)
  99. return 0; /* nothing to do */
  100. if (cb->len) {
  101. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  102. sent = tty->ops->write(tty, cb->buf + cb->offset, cb->len);
  103. if (sent < 0) {
  104. /* error */
  105. gig_dbg(DEBUG_OUTPUT, "send_cb: write error %d", sent);
  106. flush_send_queue(cs);
  107. return sent;
  108. }
  109. cb->offset += sent;
  110. cb->len -= sent;
  111. gig_dbg(DEBUG_OUTPUT, "send_cb: sent %d, left %u, queued %u",
  112. sent, cb->len, cs->cmdbytes);
  113. }
  114. while (cb && !cb->len) {
  115. spin_lock_irqsave(&cs->cmdlock, flags);
  116. cs->cmdbytes -= cs->curlen;
  117. tcb = cb;
  118. cs->cmdbuf = cb = cb->next;
  119. if (cb) {
  120. cb->prev = NULL;
  121. cs->curlen = cb->len;
  122. } else {
  123. cs->lastcmdbuf = NULL;
  124. cs->curlen = 0;
  125. }
  126. spin_unlock_irqrestore(&cs->cmdlock, flags);
  127. if (tcb->wake_tasklet)
  128. tasklet_schedule(tcb->wake_tasklet);
  129. kfree(tcb);
  130. }
  131. return sent;
  132. }
  133. /*
  134. * send queue tasklet
  135. * If there is already a skb opened, put data to the transfer buffer
  136. * by calling "write_modem".
  137. * Otherwise take a new skb out of the queue.
  138. */
  139. static void gigaset_modem_fill(unsigned long data)
  140. {
  141. struct cardstate *cs = (struct cardstate *) data;
  142. struct bc_state *bcs;
  143. struct sk_buff *nextskb;
  144. int sent = 0;
  145. if (!cs) {
  146. gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
  147. return;
  148. }
  149. bcs = cs->bcs;
  150. if (!bcs) {
  151. gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
  152. return;
  153. }
  154. if (!bcs->tx_skb) {
  155. /* no skb is being sent; send command if any */
  156. sent = send_cb(cs);
  157. gig_dbg(DEBUG_OUTPUT, "%s: send_cb -> %d", __func__, sent);
  158. if (sent)
  159. /* something sent or error */
  160. return;
  161. /* no command to send; get skb */
  162. nextskb = skb_dequeue(&bcs->squeue);
  163. if (!nextskb)
  164. /* no skb either, nothing to do */
  165. return;
  166. bcs->tx_skb = nextskb;
  167. gig_dbg(DEBUG_INTR, "Dequeued skb (Adr: %lx)",
  168. (unsigned long) bcs->tx_skb);
  169. }
  170. /* send skb */
  171. gig_dbg(DEBUG_OUTPUT, "%s: tx_skb", __func__);
  172. if (write_modem(cs) < 0)
  173. gig_dbg(DEBUG_OUTPUT, "%s: write_modem failed", __func__);
  174. }
  175. /*
  176. * throw away all data queued for sending
  177. */
  178. static void flush_send_queue(struct cardstate *cs)
  179. {
  180. struct sk_buff *skb;
  181. struct cmdbuf_t *cb;
  182. unsigned long flags;
  183. /* command queue */
  184. spin_lock_irqsave(&cs->cmdlock, flags);
  185. while ((cb = cs->cmdbuf) != NULL) {
  186. cs->cmdbuf = cb->next;
  187. if (cb->wake_tasklet)
  188. tasklet_schedule(cb->wake_tasklet);
  189. kfree(cb);
  190. }
  191. cs->cmdbuf = cs->lastcmdbuf = NULL;
  192. cs->cmdbytes = cs->curlen = 0;
  193. spin_unlock_irqrestore(&cs->cmdlock, flags);
  194. /* data queue */
  195. if (cs->bcs->tx_skb)
  196. dev_kfree_skb_any(cs->bcs->tx_skb);
  197. while ((skb = skb_dequeue(&cs->bcs->squeue)) != NULL)
  198. dev_kfree_skb_any(skb);
  199. }
  200. /* Gigaset Driver Interface */
  201. /* ======================== */
  202. /*
  203. * queue an AT command string for transmission to the Gigaset device
  204. * parameters:
  205. * cs controller state structure
  206. * buf buffer containing the string to send
  207. * len number of characters to send
  208. * wake_tasklet tasklet to run when transmission is complete, or NULL
  209. * return value:
  210. * number of bytes queued, or error code < 0
  211. */
  212. static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
  213. {
  214. unsigned long flags;
  215. gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
  216. DEBUG_TRANSCMD : DEBUG_LOCKCMD,
  217. "CMD Transmit", cb->len, cb->buf);
  218. spin_lock_irqsave(&cs->cmdlock, flags);
  219. cb->prev = cs->lastcmdbuf;
  220. if (cs->lastcmdbuf)
  221. cs->lastcmdbuf->next = cb;
  222. else {
  223. cs->cmdbuf = cb;
  224. cs->curlen = cb->len;
  225. }
  226. cs->cmdbytes += cb->len;
  227. cs->lastcmdbuf = cb;
  228. spin_unlock_irqrestore(&cs->cmdlock, flags);
  229. spin_lock_irqsave(&cs->lock, flags);
  230. if (cs->connected)
  231. tasklet_schedule(&cs->write_tasklet);
  232. spin_unlock_irqrestore(&cs->lock, flags);
  233. return cb->len;
  234. }
  235. /*
  236. * tty_driver.write_room interface routine
  237. * return number of characters the driver will accept to be written
  238. * parameter:
  239. * controller state structure
  240. * return value:
  241. * number of characters
  242. */
  243. static int gigaset_write_room(struct cardstate *cs)
  244. {
  245. unsigned bytes;
  246. bytes = cs->cmdbytes;
  247. return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
  248. }
  249. /*
  250. * tty_driver.chars_in_buffer interface routine
  251. * return number of characters waiting to be sent
  252. * parameter:
  253. * controller state structure
  254. * return value:
  255. * number of characters
  256. */
  257. static int gigaset_chars_in_buffer(struct cardstate *cs)
  258. {
  259. return cs->cmdbytes;
  260. }
  261. /*
  262. * implementation of ioctl(GIGASET_BRKCHARS)
  263. * parameter:
  264. * controller state structure
  265. * return value:
  266. * -EINVAL (unimplemented function)
  267. */
  268. static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
  269. {
  270. /* not implemented */
  271. return -EINVAL;
  272. }
  273. /*
  274. * Open B channel
  275. * Called by "do_action" in ev-layer.c
  276. */
  277. static int gigaset_init_bchannel(struct bc_state *bcs)
  278. {
  279. /* nothing to do for M10x */
  280. gigaset_bchannel_up(bcs);
  281. return 0;
  282. }
  283. /*
  284. * Close B channel
  285. * Called by "do_action" in ev-layer.c
  286. */
  287. static int gigaset_close_bchannel(struct bc_state *bcs)
  288. {
  289. /* nothing to do for M10x */
  290. gigaset_bchannel_down(bcs);
  291. return 0;
  292. }
  293. /*
  294. * Set up B channel structure
  295. * This is called by "gigaset_initcs" in common.c
  296. */
  297. static int gigaset_initbcshw(struct bc_state *bcs)
  298. {
  299. /* unused */
  300. bcs->hw.ser = NULL;
  301. return 1;
  302. }
  303. /*
  304. * Free B channel structure
  305. * Called by "gigaset_freebcs" in common.c
  306. */
  307. static int gigaset_freebcshw(struct bc_state *bcs)
  308. {
  309. /* unused */
  310. return 1;
  311. }
  312. /*
  313. * Reinitialize B channel structure
  314. * This is called by "bcs_reinit" in common.c
  315. */
  316. static void gigaset_reinitbcshw(struct bc_state *bcs)
  317. {
  318. /* nothing to do for M10x */
  319. }
  320. /*
  321. * Free hardware specific device data
  322. * This will be called by "gigaset_freecs" in common.c
  323. */
  324. static void gigaset_freecshw(struct cardstate *cs)
  325. {
  326. tasklet_kill(&cs->write_tasklet);
  327. if (!cs->hw.ser)
  328. return;
  329. platform_device_unregister(&cs->hw.ser->dev);
  330. }
  331. static void gigaset_device_release(struct device *dev)
  332. {
  333. kfree(container_of(dev, struct ser_cardstate, dev.dev));
  334. }
  335. /*
  336. * Set up hardware specific device data
  337. * This is called by "gigaset_initcs" in common.c
  338. */
  339. static int gigaset_initcshw(struct cardstate *cs)
  340. {
  341. int rc;
  342. struct ser_cardstate *scs;
  343. scs = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL);
  344. if (!scs) {
  345. pr_err("out of memory\n");
  346. return 0;
  347. }
  348. cs->hw.ser = scs;
  349. cs->hw.ser->dev.name = GIGASET_MODULENAME;
  350. cs->hw.ser->dev.id = cs->minor_index;
  351. cs->hw.ser->dev.dev.release = gigaset_device_release;
  352. rc = platform_device_register(&cs->hw.ser->dev);
  353. if (rc != 0) {
  354. pr_err("error %d registering platform device\n", rc);
  355. kfree(cs->hw.ser);
  356. cs->hw.ser = NULL;
  357. return 0;
  358. }
  359. tasklet_init(&cs->write_tasklet,
  360. gigaset_modem_fill, (unsigned long) cs);
  361. return 1;
  362. }
  363. /*
  364. * set modem control lines
  365. * Parameters:
  366. * card state structure
  367. * modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
  368. * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
  369. * and by "if_lock" and "if_termios" in interface.c
  370. */
  371. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  372. unsigned new_state)
  373. {
  374. struct tty_struct *tty = cs->hw.ser->tty;
  375. unsigned int set, clear;
  376. if (!tty || !tty->driver || !tty->ops->tiocmset)
  377. return -EINVAL;
  378. set = new_state & ~old_state;
  379. clear = old_state & ~new_state;
  380. if (!set && !clear)
  381. return 0;
  382. gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
  383. return tty->ops->tiocmset(tty, set, clear);
  384. }
  385. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  386. {
  387. return -EINVAL;
  388. }
  389. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  390. {
  391. return -EINVAL;
  392. }
  393. static const struct gigaset_ops ops = {
  394. gigaset_write_cmd,
  395. gigaset_write_room,
  396. gigaset_chars_in_buffer,
  397. gigaset_brkchars,
  398. gigaset_init_bchannel,
  399. gigaset_close_bchannel,
  400. gigaset_initbcshw,
  401. gigaset_freebcshw,
  402. gigaset_reinitbcshw,
  403. gigaset_initcshw,
  404. gigaset_freecshw,
  405. gigaset_set_modem_ctrl,
  406. gigaset_baud_rate,
  407. gigaset_set_line_ctrl,
  408. gigaset_m10x_send_skb, /* asyncdata.c */
  409. gigaset_m10x_input, /* asyncdata.c */
  410. };
  411. /* Line Discipline Interface */
  412. /* ========================= */
  413. /* helper functions for cardstate refcounting */
  414. static struct cardstate *cs_get(struct tty_struct *tty)
  415. {
  416. struct cardstate *cs = tty->disc_data;
  417. if (!cs || !cs->hw.ser) {
  418. gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
  419. return NULL;
  420. }
  421. atomic_inc(&cs->hw.ser->refcnt);
  422. return cs;
  423. }
  424. static void cs_put(struct cardstate *cs)
  425. {
  426. if (atomic_dec_and_test(&cs->hw.ser->refcnt))
  427. complete(&cs->hw.ser->dead_cmp);
  428. }
  429. /*
  430. * Called by the tty driver when the line discipline is pushed onto the tty.
  431. * Called in process context.
  432. */
  433. static int
  434. gigaset_tty_open(struct tty_struct *tty)
  435. {
  436. struct cardstate *cs;
  437. gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
  438. pr_info(DRIVER_DESC "\n");
  439. if (!driver) {
  440. pr_err("%s: no driver structure\n", __func__);
  441. return -ENODEV;
  442. }
  443. /* allocate memory for our device state and initialize it */
  444. cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
  445. if (!cs)
  446. goto error;
  447. cs->dev = &cs->hw.ser->dev.dev;
  448. cs->hw.ser->tty = tty;
  449. atomic_set(&cs->hw.ser->refcnt, 1);
  450. init_completion(&cs->hw.ser->dead_cmp);
  451. tty->disc_data = cs;
  452. /* OK.. Initialization of the datastructures and the HW is done.. Now
  453. * startup system and notify the LL that we are ready to run
  454. */
  455. if (startmode == SM_LOCKED)
  456. cs->mstate = MS_LOCKED;
  457. if (!gigaset_start(cs)) {
  458. tasklet_kill(&cs->write_tasklet);
  459. goto error;
  460. }
  461. gig_dbg(DEBUG_INIT, "Startup of HLL done");
  462. return 0;
  463. error:
  464. gig_dbg(DEBUG_INIT, "Startup of HLL failed");
  465. tty->disc_data = NULL;
  466. gigaset_freecs(cs);
  467. return -ENODEV;
  468. }
  469. /*
  470. * Called by the tty driver when the line discipline is removed.
  471. * Called from process context.
  472. */
  473. static void
  474. gigaset_tty_close(struct tty_struct *tty)
  475. {
  476. struct cardstate *cs = tty->disc_data;
  477. gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
  478. if (!cs) {
  479. gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
  480. return;
  481. }
  482. /* prevent other callers from entering ldisc methods */
  483. tty->disc_data = NULL;
  484. if (!cs->hw.ser)
  485. pr_err("%s: no hw cardstate\n", __func__);
  486. else {
  487. /* wait for running methods to finish */
  488. if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
  489. wait_for_completion(&cs->hw.ser->dead_cmp);
  490. }
  491. /* stop operations */
  492. gigaset_stop(cs);
  493. tasklet_kill(&cs->write_tasklet);
  494. flush_send_queue(cs);
  495. cs->dev = NULL;
  496. gigaset_freecs(cs);
  497. gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
  498. }
  499. /*
  500. * Called by the tty driver when the tty line is hung up.
  501. * Wait for I/O to driver to complete and unregister ISDN device.
  502. * This is already done by the close routine, so just call that.
  503. * Called from process context.
  504. */
  505. static int gigaset_tty_hangup(struct tty_struct *tty)
  506. {
  507. gigaset_tty_close(tty);
  508. return 0;
  509. }
  510. /*
  511. * Read on the tty.
  512. * Unused, received data goes only to the Gigaset driver.
  513. */
  514. static ssize_t
  515. gigaset_tty_read(struct tty_struct *tty, struct file *file,
  516. unsigned char __user *buf, size_t count)
  517. {
  518. return -EAGAIN;
  519. }
  520. /*
  521. * Write on the tty.
  522. * Unused, transmit data comes only from the Gigaset driver.
  523. */
  524. static ssize_t
  525. gigaset_tty_write(struct tty_struct *tty, struct file *file,
  526. const unsigned char *buf, size_t count)
  527. {
  528. return -EAGAIN;
  529. }
  530. /*
  531. * Ioctl on the tty.
  532. * Called in process context only.
  533. * May be re-entered by multiple ioctl calling threads.
  534. */
  535. static int
  536. gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
  537. unsigned int cmd, unsigned long arg)
  538. {
  539. struct cardstate *cs = cs_get(tty);
  540. int rc, val;
  541. int __user *p = (int __user *)arg;
  542. if (!cs)
  543. return -ENXIO;
  544. switch (cmd) {
  545. case FIONREAD:
  546. /* unused, always return zero */
  547. val = 0;
  548. rc = put_user(val, p);
  549. break;
  550. case TCFLSH:
  551. /* flush our buffers and the serial port's buffer */
  552. switch (arg) {
  553. case TCIFLUSH:
  554. /* no own input buffer to flush */
  555. break;
  556. case TCIOFLUSH:
  557. case TCOFLUSH:
  558. flush_send_queue(cs);
  559. break;
  560. }
  561. /* Pass through */
  562. default:
  563. /* pass through to underlying serial device */
  564. rc = n_tty_ioctl_helper(tty, file, cmd, arg);
  565. break;
  566. }
  567. cs_put(cs);
  568. return rc;
  569. }
  570. /*
  571. * Called by the tty driver when a block of data has been received.
  572. * Will not be re-entered while running but other ldisc functions
  573. * may be called in parallel.
  574. * Can be called from hard interrupt level as well as soft interrupt
  575. * level or mainline.
  576. * Parameters:
  577. * tty tty structure
  578. * buf buffer containing received characters
  579. * cflags buffer containing error flags for received characters (ignored)
  580. * count number of received characters
  581. */
  582. static void
  583. gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
  584. char *cflags, int count)
  585. {
  586. struct cardstate *cs = cs_get(tty);
  587. unsigned tail, head, n;
  588. struct inbuf_t *inbuf;
  589. if (!cs)
  590. return;
  591. inbuf = cs->inbuf;
  592. if (!inbuf) {
  593. dev_err(cs->dev, "%s: no inbuf\n", __func__);
  594. cs_put(cs);
  595. return;
  596. }
  597. tail = inbuf->tail;
  598. head = inbuf->head;
  599. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
  600. head, tail, count);
  601. if (head <= tail) {
  602. /* possible buffer wraparound */
  603. n = min_t(unsigned, count, RBUFSIZE - tail);
  604. memcpy(inbuf->data + tail, buf, n);
  605. tail = (tail + n) % RBUFSIZE;
  606. buf += n;
  607. count -= n;
  608. }
  609. if (count > 0) {
  610. /* tail < head and some data left */
  611. n = head - tail - 1;
  612. if (count > n) {
  613. dev_err(cs->dev,
  614. "inbuf overflow, discarding %d bytes\n",
  615. count - n);
  616. count = n;
  617. }
  618. memcpy(inbuf->data + tail, buf, count);
  619. tail += count;
  620. }
  621. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  622. inbuf->tail = tail;
  623. /* Everything was received .. Push data into handler */
  624. gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
  625. gigaset_schedule_event(cs);
  626. cs_put(cs);
  627. }
  628. /*
  629. * Called by the tty driver when there's room for more data to send.
  630. */
  631. static void
  632. gigaset_tty_wakeup(struct tty_struct *tty)
  633. {
  634. struct cardstate *cs = cs_get(tty);
  635. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  636. if (!cs)
  637. return;
  638. tasklet_schedule(&cs->write_tasklet);
  639. cs_put(cs);
  640. }
  641. static struct tty_ldisc_ops gigaset_ldisc = {
  642. .owner = THIS_MODULE,
  643. .magic = TTY_LDISC_MAGIC,
  644. .name = "ser_gigaset",
  645. .open = gigaset_tty_open,
  646. .close = gigaset_tty_close,
  647. .hangup = gigaset_tty_hangup,
  648. .read = gigaset_tty_read,
  649. .write = gigaset_tty_write,
  650. .ioctl = gigaset_tty_ioctl,
  651. .receive_buf = gigaset_tty_receive,
  652. .write_wakeup = gigaset_tty_wakeup,
  653. };
  654. /* Initialization / Shutdown */
  655. /* ========================= */
  656. static int __init ser_gigaset_init(void)
  657. {
  658. int rc;
  659. gig_dbg(DEBUG_INIT, "%s", __func__);
  660. rc = platform_driver_register(&device_driver);
  661. if (rc != 0) {
  662. pr_err("error %d registering platform driver\n", rc);
  663. return rc;
  664. }
  665. /* allocate memory for our driver state and initialize it */
  666. driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
  667. GIGASET_MODULENAME, GIGASET_DEVNAME,
  668. &ops, THIS_MODULE);
  669. if (!driver)
  670. goto error;
  671. rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc);
  672. if (rc != 0) {
  673. pr_err("error %d registering line discipline\n", rc);
  674. goto error;
  675. }
  676. return 0;
  677. error:
  678. if (driver) {
  679. gigaset_freedriver(driver);
  680. driver = NULL;
  681. }
  682. platform_driver_unregister(&device_driver);
  683. return rc;
  684. }
  685. static void __exit ser_gigaset_exit(void)
  686. {
  687. int rc;
  688. gig_dbg(DEBUG_INIT, "%s", __func__);
  689. if (driver) {
  690. gigaset_freedriver(driver);
  691. driver = NULL;
  692. }
  693. rc = tty_unregister_ldisc(N_GIGASET_M101);
  694. if (rc != 0)
  695. pr_err("error %d unregistering line discipline\n", rc);
  696. platform_driver_unregister(&device_driver);
  697. }
  698. module_init(ser_gigaset_init);
  699. module_exit(ser_gigaset_exit);