ser-gigaset.c 19 KB

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