ser-gigaset.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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/poll.h>
  18. #include <linux/completion.h>
  19. /* Version Information */
  20. #define DRIVER_AUTHOR "Tilman Schmidt"
  21. #define DRIVER_DESC "Serial Driver for Gigaset 307x using Siemens M101"
  22. #define GIGASET_MINORS 1
  23. #define GIGASET_MINOR 0
  24. #define GIGASET_MODULENAME "ser_gigaset"
  25. #define GIGASET_DEVNAME "ttyGS"
  26. /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
  27. #define IF_WRITEBUF 264
  28. MODULE_AUTHOR(DRIVER_AUTHOR);
  29. MODULE_DESCRIPTION(DRIVER_DESC);
  30. MODULE_LICENSE("GPL");
  31. MODULE_ALIAS_LDISC(N_GIGASET_M101);
  32. static int startmode = SM_ISDN;
  33. module_param(startmode, int, S_IRUGO);
  34. MODULE_PARM_DESC(startmode, "initial operation mode");
  35. static int cidmode = 1;
  36. module_param(cidmode, int, S_IRUGO);
  37. MODULE_PARM_DESC(cidmode, "stay in CID mode when idle");
  38. static struct gigaset_driver *driver;
  39. struct ser_cardstate {
  40. struct platform_device dev;
  41. struct tty_struct *tty;
  42. atomic_t refcnt;
  43. struct completion dead_cmp;
  44. };
  45. static struct platform_driver device_driver = {
  46. .driver = {
  47. .name = GIGASET_MODULENAME,
  48. },
  49. };
  50. static void flush_send_queue(struct cardstate *);
  51. /* transmit data from current open skb
  52. * result: number of bytes sent or error code < 0
  53. */
  54. static int write_modem(struct cardstate *cs)
  55. {
  56. struct tty_struct *tty = cs->hw.ser->tty;
  57. struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
  58. struct sk_buff *skb = bcs->tx_skb;
  59. int sent;
  60. if (!tty || !tty->driver || !skb)
  61. return -EFAULT;
  62. if (!skb->len) {
  63. dev_kfree_skb_any(skb);
  64. bcs->tx_skb = NULL;
  65. return -EINVAL;
  66. }
  67. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  68. sent = tty->driver->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->driver->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 =
  343. container_of(dev, struct platform_device, dev);
  344. /* adapted from platform_device_release() in drivers/base/platform.c */
  345. //FIXME is this actually necessary?
  346. kfree(dev->platform_data);
  347. kfree(pdev->resource);
  348. }
  349. /*
  350. * Set up hardware specific device data
  351. * This is called by "gigaset_initcs" in common.c
  352. */
  353. static int gigaset_initcshw(struct cardstate *cs)
  354. {
  355. int rc;
  356. if (!(cs->hw.ser = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL))) {
  357. err("%s: out of memory!", __func__);
  358. return 0;
  359. }
  360. cs->hw.ser->dev.name = GIGASET_MODULENAME;
  361. cs->hw.ser->dev.id = cs->minor_index;
  362. cs->hw.ser->dev.dev.release = gigaset_device_release;
  363. if ((rc = platform_device_register(&cs->hw.ser->dev)) != 0) {
  364. err("error %d registering platform device", rc);
  365. kfree(cs->hw.ser);
  366. cs->hw.ser = NULL;
  367. return 0;
  368. }
  369. dev_set_drvdata(&cs->hw.ser->dev.dev, cs);
  370. tasklet_init(&cs->write_tasklet,
  371. &gigaset_modem_fill, (unsigned long) cs);
  372. return 1;
  373. }
  374. /*
  375. * set modem control lines
  376. * Parameters:
  377. * card state structure
  378. * modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
  379. * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
  380. * and by "if_lock" and "if_termios" in interface.c
  381. */
  382. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state, unsigned new_state)
  383. {
  384. struct tty_struct *tty = cs->hw.ser->tty;
  385. unsigned int set, clear;
  386. if (!tty || !tty->driver || !tty->driver->tiocmset)
  387. return -EFAULT;
  388. set = new_state & ~old_state;
  389. clear = old_state & ~new_state;
  390. if (!set && !clear)
  391. return 0;
  392. gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
  393. return tty->driver->tiocmset(tty, NULL, set, clear);
  394. }
  395. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  396. {
  397. return -EINVAL;
  398. }
  399. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  400. {
  401. return -EINVAL;
  402. }
  403. static const struct gigaset_ops ops = {
  404. gigaset_write_cmd,
  405. gigaset_write_room,
  406. gigaset_chars_in_buffer,
  407. gigaset_brkchars,
  408. gigaset_init_bchannel,
  409. gigaset_close_bchannel,
  410. gigaset_initbcshw,
  411. gigaset_freebcshw,
  412. gigaset_reinitbcshw,
  413. gigaset_initcshw,
  414. gigaset_freecshw,
  415. gigaset_set_modem_ctrl,
  416. gigaset_baud_rate,
  417. gigaset_set_line_ctrl,
  418. gigaset_m10x_send_skb, /* asyncdata.c */
  419. gigaset_m10x_input, /* asyncdata.c */
  420. };
  421. /* Line Discipline Interface */
  422. /* ========================= */
  423. /* helper functions for cardstate refcounting */
  424. static struct cardstate *cs_get(struct tty_struct *tty)
  425. {
  426. struct cardstate *cs = tty->disc_data;
  427. if (!cs || !cs->hw.ser) {
  428. gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
  429. return NULL;
  430. }
  431. atomic_inc(&cs->hw.ser->refcnt);
  432. return cs;
  433. }
  434. static void cs_put(struct cardstate *cs)
  435. {
  436. if (atomic_dec_and_test(&cs->hw.ser->refcnt))
  437. complete(&cs->hw.ser->dead_cmp);
  438. }
  439. /*
  440. * Called by the tty driver when the line discipline is pushed onto the tty.
  441. * Called in process context.
  442. */
  443. static int
  444. gigaset_tty_open(struct tty_struct *tty)
  445. {
  446. struct cardstate *cs;
  447. gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
  448. info(DRIVER_AUTHOR);
  449. info(DRIVER_DESC);
  450. if (!driver) {
  451. err("%s: no driver structure", __func__);
  452. return -ENODEV;
  453. }
  454. /* allocate memory for our device state and intialize it */
  455. if (!(cs = gigaset_initcs(driver, 1, 1, 0, cidmode,
  456. GIGASET_MODULENAME)))
  457. goto error;
  458. cs->dev = &cs->hw.ser->dev.dev;
  459. cs->hw.ser->tty = tty;
  460. atomic_set(&cs->hw.ser->refcnt, 1);
  461. init_completion(&cs->hw.ser->dead_cmp);
  462. tty->disc_data = cs;
  463. /* OK.. Initialization of the datastructures and the HW is done.. Now
  464. * startup system and notify the LL that we are ready to run
  465. */
  466. if (startmode == SM_LOCKED)
  467. cs->mstate = MS_LOCKED;
  468. if (!gigaset_start(cs)) {
  469. tasklet_kill(&cs->write_tasklet);
  470. goto error;
  471. }
  472. gig_dbg(DEBUG_INIT, "Startup of HLL done");
  473. return 0;
  474. error:
  475. gig_dbg(DEBUG_INIT, "Startup of HLL failed");
  476. tty->disc_data = NULL;
  477. gigaset_freecs(cs);
  478. return -ENODEV;
  479. }
  480. /*
  481. * Called by the tty driver when the line discipline is removed.
  482. * Called from process context.
  483. */
  484. static void
  485. gigaset_tty_close(struct tty_struct *tty)
  486. {
  487. struct cardstate *cs = tty->disc_data;
  488. gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
  489. if (!cs) {
  490. gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
  491. return;
  492. }
  493. /* prevent other callers from entering ldisc methods */
  494. tty->disc_data = NULL;
  495. if (!cs->hw.ser)
  496. err("%s: no hw cardstate", __func__);
  497. else {
  498. /* wait for running methods to finish */
  499. if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
  500. wait_for_completion(&cs->hw.ser->dead_cmp);
  501. }
  502. /* stop operations */
  503. gigaset_stop(cs);
  504. tasklet_kill(&cs->write_tasklet);
  505. flush_send_queue(cs);
  506. cs->dev = NULL;
  507. gigaset_freecs(cs);
  508. gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
  509. }
  510. /*
  511. * Called by the tty driver when the tty line is hung up.
  512. * Wait for I/O to driver to complete and unregister ISDN device.
  513. * This is already done by the close routine, so just call that.
  514. * Called from process context.
  515. */
  516. static int gigaset_tty_hangup(struct tty_struct *tty)
  517. {
  518. gigaset_tty_close(tty);
  519. return 0;
  520. }
  521. /*
  522. * Read on the tty.
  523. * Unused, received data goes only to the Gigaset driver.
  524. */
  525. static ssize_t
  526. gigaset_tty_read(struct tty_struct *tty, struct file *file,
  527. unsigned char __user *buf, size_t count)
  528. {
  529. return -EAGAIN;
  530. }
  531. /*
  532. * Write on the tty.
  533. * Unused, transmit data comes only from the Gigaset driver.
  534. */
  535. static ssize_t
  536. gigaset_tty_write(struct tty_struct *tty, struct file *file,
  537. const unsigned char *buf, size_t count)
  538. {
  539. return -EAGAIN;
  540. }
  541. /*
  542. * Ioctl on the tty.
  543. * Called in process context only.
  544. * May be re-entered by multiple ioctl calling threads.
  545. */
  546. static int
  547. gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
  548. unsigned int cmd, unsigned long arg)
  549. {
  550. struct cardstate *cs = cs_get(tty);
  551. int rc, val;
  552. int __user *p = (int __user *)arg;
  553. if (!cs)
  554. return -ENXIO;
  555. switch (cmd) {
  556. case TCGETS:
  557. case TCGETA:
  558. /* pass through to underlying serial device */
  559. rc = n_tty_ioctl(tty, file, cmd, arg);
  560. break;
  561. case TCFLSH:
  562. /* flush our buffers and the serial port's buffer */
  563. switch (arg) {
  564. case TCIFLUSH:
  565. /* no own input buffer to flush */
  566. break;
  567. case TCIOFLUSH:
  568. case TCOFLUSH:
  569. flush_send_queue(cs);
  570. break;
  571. }
  572. /* flush the serial port's buffer */
  573. rc = n_tty_ioctl(tty, file, cmd, arg);
  574. break;
  575. case FIONREAD:
  576. /* unused, always return zero */
  577. val = 0;
  578. rc = put_user(val, p);
  579. break;
  580. default:
  581. rc = -ENOIOCTLCMD;
  582. }
  583. cs_put(cs);
  584. return rc;
  585. }
  586. /*
  587. * Poll on the tty.
  588. * Unused, always return zero.
  589. */
  590. static unsigned int
  591. gigaset_tty_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
  592. {
  593. return 0;
  594. }
  595. /*
  596. * Called by the tty driver when a block of data has been received.
  597. * Will not be re-entered while running but other ldisc functions
  598. * may be called in parallel.
  599. * Can be called from hard interrupt level as well as soft interrupt
  600. * level or mainline.
  601. * Parameters:
  602. * tty tty structure
  603. * buf buffer containing received characters
  604. * cflags buffer containing error flags for received characters (ignored)
  605. * count number of received characters
  606. */
  607. static void
  608. gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
  609. char *cflags, int count)
  610. {
  611. struct cardstate *cs = cs_get(tty);
  612. unsigned tail, head, n;
  613. struct inbuf_t *inbuf;
  614. if (!cs)
  615. return;
  616. if (!(inbuf = cs->inbuf)) {
  617. dev_err(cs->dev, "%s: no inbuf\n", __func__);
  618. cs_put(cs);
  619. return;
  620. }
  621. tail = inbuf->tail;
  622. head = inbuf->head;
  623. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
  624. head, tail, count);
  625. if (head <= tail) {
  626. /* possible buffer wraparound */
  627. n = min_t(unsigned, count, RBUFSIZE - tail);
  628. memcpy(inbuf->data + tail, buf, n);
  629. tail = (tail + n) % RBUFSIZE;
  630. buf += n;
  631. count -= n;
  632. }
  633. if (count > 0) {
  634. /* tail < head and some data left */
  635. n = head - tail - 1;
  636. if (count > n) {
  637. dev_err(cs->dev,
  638. "inbuf overflow, discarding %d bytes\n",
  639. count - n);
  640. count = n;
  641. }
  642. memcpy(inbuf->data + tail, buf, count);
  643. tail += count;
  644. }
  645. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  646. inbuf->tail = tail;
  647. /* Everything was received .. Push data into handler */
  648. gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
  649. gigaset_schedule_event(cs);
  650. cs_put(cs);
  651. }
  652. /*
  653. * Called by the tty driver when there's room for more data to send.
  654. */
  655. static void
  656. gigaset_tty_wakeup(struct tty_struct *tty)
  657. {
  658. struct cardstate *cs = cs_get(tty);
  659. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  660. if (!cs)
  661. return;
  662. tasklet_schedule(&cs->write_tasklet);
  663. cs_put(cs);
  664. }
  665. static struct tty_ldisc gigaset_ldisc = {
  666. .owner = THIS_MODULE,
  667. .magic = TTY_LDISC_MAGIC,
  668. .name = "ser_gigaset",
  669. .open = gigaset_tty_open,
  670. .close = gigaset_tty_close,
  671. .hangup = gigaset_tty_hangup,
  672. .read = gigaset_tty_read,
  673. .write = gigaset_tty_write,
  674. .ioctl = gigaset_tty_ioctl,
  675. .poll = gigaset_tty_poll,
  676. .receive_buf = gigaset_tty_receive,
  677. .write_wakeup = gigaset_tty_wakeup,
  678. };
  679. /* Initialization / Shutdown */
  680. /* ========================= */
  681. static int __init ser_gigaset_init(void)
  682. {
  683. int rc;
  684. gig_dbg(DEBUG_INIT, "%s", __func__);
  685. if ((rc = platform_driver_register(&device_driver)) != 0) {
  686. err("error %d registering platform driver", rc);
  687. return rc;
  688. }
  689. /* allocate memory for our driver state and intialize it */
  690. if (!(driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
  691. GIGASET_MODULENAME, GIGASET_DEVNAME,
  692. &ops, THIS_MODULE)))
  693. goto error;
  694. if ((rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc)) != 0) {
  695. err("error %d registering line discipline", 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. if ((rc = tty_unregister_ldisc(N_GIGASET_M101)) != 0)
  716. err("error %d unregistering line discipline", rc);
  717. platform_driver_unregister(&device_driver);
  718. }
  719. module_init(ser_gigaset_init);
  720. module_exit(ser_gigaset_exit);