ser-gigaset.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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, const unsigned char *buf,
  213. int len, struct tasklet_struct *wake_tasklet)
  214. {
  215. struct cmdbuf_t *cb;
  216. unsigned long flags;
  217. gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
  218. DEBUG_TRANSCMD : DEBUG_LOCKCMD,
  219. "CMD Transmit", len, buf);
  220. if (len <= 0)
  221. return 0;
  222. cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC);
  223. if (!cb) {
  224. dev_err(cs->dev, "%s: out of memory!\n", __func__);
  225. return -ENOMEM;
  226. }
  227. memcpy(cb->buf, buf, len);
  228. cb->len = len;
  229. cb->offset = 0;
  230. cb->next = NULL;
  231. cb->wake_tasklet = wake_tasklet;
  232. spin_lock_irqsave(&cs->cmdlock, flags);
  233. cb->prev = cs->lastcmdbuf;
  234. if (cs->lastcmdbuf)
  235. cs->lastcmdbuf->next = cb;
  236. else {
  237. cs->cmdbuf = cb;
  238. cs->curlen = len;
  239. }
  240. cs->cmdbytes += len;
  241. cs->lastcmdbuf = cb;
  242. spin_unlock_irqrestore(&cs->cmdlock, flags);
  243. spin_lock_irqsave(&cs->lock, flags);
  244. if (cs->connected)
  245. tasklet_schedule(&cs->write_tasklet);
  246. spin_unlock_irqrestore(&cs->lock, flags);
  247. return len;
  248. }
  249. /*
  250. * tty_driver.write_room interface routine
  251. * return number of characters the driver will accept to be written
  252. * parameter:
  253. * controller state structure
  254. * return value:
  255. * number of characters
  256. */
  257. static int gigaset_write_room(struct cardstate *cs)
  258. {
  259. unsigned bytes;
  260. bytes = cs->cmdbytes;
  261. return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
  262. }
  263. /*
  264. * tty_driver.chars_in_buffer interface routine
  265. * return number of characters waiting to be sent
  266. * parameter:
  267. * controller state structure
  268. * return value:
  269. * number of characters
  270. */
  271. static int gigaset_chars_in_buffer(struct cardstate *cs)
  272. {
  273. return cs->cmdbytes;
  274. }
  275. /*
  276. * implementation of ioctl(GIGASET_BRKCHARS)
  277. * parameter:
  278. * controller state structure
  279. * return value:
  280. * -EINVAL (unimplemented function)
  281. */
  282. static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
  283. {
  284. /* not implemented */
  285. return -EINVAL;
  286. }
  287. /*
  288. * Open B channel
  289. * Called by "do_action" in ev-layer.c
  290. */
  291. static int gigaset_init_bchannel(struct bc_state *bcs)
  292. {
  293. /* nothing to do for M10x */
  294. gigaset_bchannel_up(bcs);
  295. return 0;
  296. }
  297. /*
  298. * Close B channel
  299. * Called by "do_action" in ev-layer.c
  300. */
  301. static int gigaset_close_bchannel(struct bc_state *bcs)
  302. {
  303. /* nothing to do for M10x */
  304. gigaset_bchannel_down(bcs);
  305. return 0;
  306. }
  307. /*
  308. * Set up B channel structure
  309. * This is called by "gigaset_initcs" in common.c
  310. */
  311. static int gigaset_initbcshw(struct bc_state *bcs)
  312. {
  313. /* unused */
  314. bcs->hw.ser = NULL;
  315. return 1;
  316. }
  317. /*
  318. * Free B channel structure
  319. * Called by "gigaset_freebcs" in common.c
  320. */
  321. static int gigaset_freebcshw(struct bc_state *bcs)
  322. {
  323. /* unused */
  324. return 1;
  325. }
  326. /*
  327. * Reinitialize B channel structure
  328. * This is called by "bcs_reinit" in common.c
  329. */
  330. static void gigaset_reinitbcshw(struct bc_state *bcs)
  331. {
  332. /* nothing to do for M10x */
  333. }
  334. /*
  335. * Free hardware specific device data
  336. * This will be called by "gigaset_freecs" in common.c
  337. */
  338. static void gigaset_freecshw(struct cardstate *cs)
  339. {
  340. tasklet_kill(&cs->write_tasklet);
  341. if (!cs->hw.ser)
  342. return;
  343. dev_set_drvdata(&cs->hw.ser->dev.dev, NULL);
  344. platform_device_unregister(&cs->hw.ser->dev);
  345. kfree(cs->hw.ser);
  346. cs->hw.ser = NULL;
  347. }
  348. static void gigaset_device_release(struct device *dev)
  349. {
  350. struct platform_device *pdev = to_platform_device(dev);
  351. /* adapted from platform_device_release() in drivers/base/platform.c */
  352. kfree(dev->platform_data);
  353. kfree(pdev->resource);
  354. }
  355. /*
  356. * Set up hardware specific device data
  357. * This is called by "gigaset_initcs" in common.c
  358. */
  359. static int gigaset_initcshw(struct cardstate *cs)
  360. {
  361. int rc;
  362. struct ser_cardstate *scs;
  363. scs = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL);
  364. if (!scs) {
  365. pr_err("out of memory\n");
  366. return 0;
  367. }
  368. cs->hw.ser = scs;
  369. cs->hw.ser->dev.name = GIGASET_MODULENAME;
  370. cs->hw.ser->dev.id = cs->minor_index;
  371. cs->hw.ser->dev.dev.release = gigaset_device_release;
  372. rc = platform_device_register(&cs->hw.ser->dev);
  373. if (rc != 0) {
  374. pr_err("error %d registering platform device\n", rc);
  375. kfree(cs->hw.ser);
  376. cs->hw.ser = NULL;
  377. return 0;
  378. }
  379. dev_set_drvdata(&cs->hw.ser->dev.dev, cs);
  380. tasklet_init(&cs->write_tasklet,
  381. gigaset_modem_fill, (unsigned long) cs);
  382. return 1;
  383. }
  384. /*
  385. * set modem control lines
  386. * Parameters:
  387. * card state structure
  388. * modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
  389. * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
  390. * and by "if_lock" and "if_termios" in interface.c
  391. */
  392. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  393. unsigned new_state)
  394. {
  395. struct tty_struct *tty = cs->hw.ser->tty;
  396. unsigned int set, clear;
  397. if (!tty || !tty->driver || !tty->ops->tiocmset)
  398. return -EINVAL;
  399. set = new_state & ~old_state;
  400. clear = old_state & ~new_state;
  401. if (!set && !clear)
  402. return 0;
  403. gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
  404. return tty->ops->tiocmset(tty, NULL, set, clear);
  405. }
  406. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  407. {
  408. return -EINVAL;
  409. }
  410. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  411. {
  412. return -EINVAL;
  413. }
  414. static const struct gigaset_ops ops = {
  415. gigaset_write_cmd,
  416. gigaset_write_room,
  417. gigaset_chars_in_buffer,
  418. gigaset_brkchars,
  419. gigaset_init_bchannel,
  420. gigaset_close_bchannel,
  421. gigaset_initbcshw,
  422. gigaset_freebcshw,
  423. gigaset_reinitbcshw,
  424. gigaset_initcshw,
  425. gigaset_freecshw,
  426. gigaset_set_modem_ctrl,
  427. gigaset_baud_rate,
  428. gigaset_set_line_ctrl,
  429. gigaset_m10x_send_skb, /* asyncdata.c */
  430. gigaset_m10x_input, /* asyncdata.c */
  431. };
  432. /* Line Discipline Interface */
  433. /* ========================= */
  434. /* helper functions for cardstate refcounting */
  435. static struct cardstate *cs_get(struct tty_struct *tty)
  436. {
  437. struct cardstate *cs = tty->disc_data;
  438. if (!cs || !cs->hw.ser) {
  439. gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
  440. return NULL;
  441. }
  442. atomic_inc(&cs->hw.ser->refcnt);
  443. return cs;
  444. }
  445. static void cs_put(struct cardstate *cs)
  446. {
  447. if (atomic_dec_and_test(&cs->hw.ser->refcnt))
  448. complete(&cs->hw.ser->dead_cmp);
  449. }
  450. /*
  451. * Called by the tty driver when the line discipline is pushed onto the tty.
  452. * Called in process context.
  453. */
  454. static int
  455. gigaset_tty_open(struct tty_struct *tty)
  456. {
  457. struct cardstate *cs;
  458. gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
  459. pr_info(DRIVER_DESC "\n");
  460. if (!driver) {
  461. pr_err("%s: no driver structure\n", __func__);
  462. return -ENODEV;
  463. }
  464. /* allocate memory for our device state and intialize it */
  465. cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
  466. if (!cs)
  467. goto error;
  468. cs->dev = &cs->hw.ser->dev.dev;
  469. cs->hw.ser->tty = tty;
  470. atomic_set(&cs->hw.ser->refcnt, 1);
  471. init_completion(&cs->hw.ser->dead_cmp);
  472. tty->disc_data = cs;
  473. /* OK.. Initialization of the datastructures and the HW is done.. Now
  474. * startup system and notify the LL that we are ready to run
  475. */
  476. if (startmode == SM_LOCKED)
  477. cs->mstate = MS_LOCKED;
  478. if (!gigaset_start(cs)) {
  479. tasklet_kill(&cs->write_tasklet);
  480. goto error;
  481. }
  482. gig_dbg(DEBUG_INIT, "Startup of HLL done");
  483. return 0;
  484. error:
  485. gig_dbg(DEBUG_INIT, "Startup of HLL failed");
  486. tty->disc_data = NULL;
  487. gigaset_freecs(cs);
  488. return -ENODEV;
  489. }
  490. /*
  491. * Called by the tty driver when the line discipline is removed.
  492. * Called from process context.
  493. */
  494. static void
  495. gigaset_tty_close(struct tty_struct *tty)
  496. {
  497. struct cardstate *cs = tty->disc_data;
  498. gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
  499. if (!cs) {
  500. gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
  501. return;
  502. }
  503. /* prevent other callers from entering ldisc methods */
  504. tty->disc_data = NULL;
  505. if (!cs->hw.ser)
  506. pr_err("%s: no hw cardstate\n", __func__);
  507. else {
  508. /* wait for running methods to finish */
  509. if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
  510. wait_for_completion(&cs->hw.ser->dead_cmp);
  511. }
  512. /* stop operations */
  513. gigaset_stop(cs);
  514. tasklet_kill(&cs->write_tasklet);
  515. flush_send_queue(cs);
  516. cs->dev = NULL;
  517. gigaset_freecs(cs);
  518. gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
  519. }
  520. /*
  521. * Called by the tty driver when the tty line is hung up.
  522. * Wait for I/O to driver to complete and unregister ISDN device.
  523. * This is already done by the close routine, so just call that.
  524. * Called from process context.
  525. */
  526. static int gigaset_tty_hangup(struct tty_struct *tty)
  527. {
  528. gigaset_tty_close(tty);
  529. return 0;
  530. }
  531. /*
  532. * Read on the tty.
  533. * Unused, received data goes only to the Gigaset driver.
  534. */
  535. static ssize_t
  536. gigaset_tty_read(struct tty_struct *tty, struct file *file,
  537. unsigned char __user *buf, size_t count)
  538. {
  539. return -EAGAIN;
  540. }
  541. /*
  542. * Write on the tty.
  543. * Unused, transmit data comes only from the Gigaset driver.
  544. */
  545. static ssize_t
  546. gigaset_tty_write(struct tty_struct *tty, struct file *file,
  547. const unsigned char *buf, size_t count)
  548. {
  549. return -EAGAIN;
  550. }
  551. /*
  552. * Ioctl on the tty.
  553. * Called in process context only.
  554. * May be re-entered by multiple ioctl calling threads.
  555. */
  556. static int
  557. gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
  558. unsigned int cmd, unsigned long arg)
  559. {
  560. struct cardstate *cs = cs_get(tty);
  561. int rc, val;
  562. int __user *p = (int __user *)arg;
  563. if (!cs)
  564. return -ENXIO;
  565. switch (cmd) {
  566. case FIONREAD:
  567. /* unused, always return zero */
  568. val = 0;
  569. rc = put_user(val, p);
  570. break;
  571. case TCFLSH:
  572. /* flush our buffers and the serial port's buffer */
  573. switch (arg) {
  574. case TCIFLUSH:
  575. /* no own input buffer to flush */
  576. break;
  577. case TCIOFLUSH:
  578. case TCOFLUSH:
  579. flush_send_queue(cs);
  580. break;
  581. }
  582. /* Pass through */
  583. default:
  584. /* pass through to underlying serial device */
  585. rc = n_tty_ioctl_helper(tty, file, cmd, arg);
  586. break;
  587. }
  588. cs_put(cs);
  589. return rc;
  590. }
  591. /*
  592. * Called by the tty driver when a block of data has been received.
  593. * Will not be re-entered while running but other ldisc functions
  594. * may be called in parallel.
  595. * Can be called from hard interrupt level as well as soft interrupt
  596. * level or mainline.
  597. * Parameters:
  598. * tty tty structure
  599. * buf buffer containing received characters
  600. * cflags buffer containing error flags for received characters (ignored)
  601. * count number of received characters
  602. */
  603. static void
  604. gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
  605. char *cflags, int count)
  606. {
  607. struct cardstate *cs = cs_get(tty);
  608. unsigned tail, head, n;
  609. struct inbuf_t *inbuf;
  610. if (!cs)
  611. return;
  612. inbuf = cs->inbuf;
  613. if (!inbuf) {
  614. dev_err(cs->dev, "%s: no inbuf\n", __func__);
  615. cs_put(cs);
  616. return;
  617. }
  618. tail = inbuf->tail;
  619. head = inbuf->head;
  620. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
  621. head, tail, count);
  622. if (head <= tail) {
  623. /* possible buffer wraparound */
  624. n = min_t(unsigned, count, RBUFSIZE - tail);
  625. memcpy(inbuf->data + tail, buf, n);
  626. tail = (tail + n) % RBUFSIZE;
  627. buf += n;
  628. count -= n;
  629. }
  630. if (count > 0) {
  631. /* tail < head and some data left */
  632. n = head - tail - 1;
  633. if (count > n) {
  634. dev_err(cs->dev,
  635. "inbuf overflow, discarding %d bytes\n",
  636. count - n);
  637. count = n;
  638. }
  639. memcpy(inbuf->data + tail, buf, count);
  640. tail += count;
  641. }
  642. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  643. inbuf->tail = tail;
  644. /* Everything was received .. Push data into handler */
  645. gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
  646. gigaset_schedule_event(cs);
  647. cs_put(cs);
  648. }
  649. /*
  650. * Called by the tty driver when there's room for more data to send.
  651. */
  652. static void
  653. gigaset_tty_wakeup(struct tty_struct *tty)
  654. {
  655. struct cardstate *cs = cs_get(tty);
  656. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  657. if (!cs)
  658. return;
  659. tasklet_schedule(&cs->write_tasklet);
  660. cs_put(cs);
  661. }
  662. static struct tty_ldisc_ops gigaset_ldisc = {
  663. .owner = THIS_MODULE,
  664. .magic = TTY_LDISC_MAGIC,
  665. .name = "ser_gigaset",
  666. .open = gigaset_tty_open,
  667. .close = gigaset_tty_close,
  668. .hangup = gigaset_tty_hangup,
  669. .read = gigaset_tty_read,
  670. .write = gigaset_tty_write,
  671. .ioctl = gigaset_tty_ioctl,
  672. .receive_buf = gigaset_tty_receive,
  673. .write_wakeup = gigaset_tty_wakeup,
  674. };
  675. /* Initialization / Shutdown */
  676. /* ========================= */
  677. static int __init ser_gigaset_init(void)
  678. {
  679. int rc;
  680. gig_dbg(DEBUG_INIT, "%s", __func__);
  681. rc = platform_driver_register(&device_driver);
  682. if (rc != 0) {
  683. pr_err("error %d registering platform driver\n", rc);
  684. return rc;
  685. }
  686. /* allocate memory for our driver state and intialize it */
  687. driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
  688. GIGASET_MODULENAME, GIGASET_DEVNAME,
  689. &ops, THIS_MODULE);
  690. if (!driver)
  691. goto error;
  692. rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc);
  693. if (rc != 0) {
  694. pr_err("error %d registering line discipline\n", rc);
  695. goto error;
  696. }
  697. return 0;
  698. error:
  699. if (driver) {
  700. gigaset_freedriver(driver);
  701. driver = NULL;
  702. }
  703. platform_driver_unregister(&device_driver);
  704. return rc;
  705. }
  706. static void __exit ser_gigaset_exit(void)
  707. {
  708. int rc;
  709. gig_dbg(DEBUG_INIT, "%s", __func__);
  710. if (driver) {
  711. gigaset_freedriver(driver);
  712. driver = NULL;
  713. }
  714. rc = tty_unregister_ldisc(N_GIGASET_M101);
  715. if (rc != 0)
  716. pr_err("error %d unregistering line discipline\n", rc);
  717. platform_driver_unregister(&device_driver);
  718. }
  719. module_init(ser_gigaset_init);
  720. module_exit(ser_gigaset_exit);