ser-gigaset.c 19 KB

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