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. /* 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 mutex dead_mutex;
  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;
  59. if (!tty || !tty->driver || !skb)
  60. return -EFAULT;
  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. sent = tty->driver->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->driver->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. int sent = 0;
  144. if (!cs || !(bcs = cs->bcs)) {
  145. gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
  146. return;
  147. }
  148. if (!bcs->tx_skb) {
  149. /* no skb is being sent; send command if any */
  150. sent = send_cb(cs);
  151. gig_dbg(DEBUG_OUTPUT, "%s: send_cb -> %d", __func__, sent);
  152. if (sent)
  153. /* something sent or error */
  154. return;
  155. /* no command to send; get skb */
  156. if (!(bcs->tx_skb = skb_dequeue(&bcs->squeue)))
  157. /* no skb either, nothing to do */
  158. return;
  159. gig_dbg(DEBUG_INTR, "Dequeued skb (Adr: %lx)",
  160. (unsigned long) bcs->tx_skb);
  161. }
  162. /* send skb */
  163. gig_dbg(DEBUG_OUTPUT, "%s: tx_skb", __func__);
  164. if (write_modem(cs) < 0)
  165. gig_dbg(DEBUG_OUTPUT, "%s: write_modem failed", __func__);
  166. }
  167. /*
  168. * throw away all data queued for sending
  169. */
  170. static void flush_send_queue(struct cardstate *cs)
  171. {
  172. struct sk_buff *skb;
  173. struct cmdbuf_t *cb;
  174. unsigned long flags;
  175. /* command queue */
  176. spin_lock_irqsave(&cs->cmdlock, flags);
  177. while ((cb = cs->cmdbuf) != NULL) {
  178. cs->cmdbuf = cb->next;
  179. if (cb->wake_tasklet)
  180. tasklet_schedule(cb->wake_tasklet);
  181. kfree(cb);
  182. }
  183. cs->cmdbuf = cs->lastcmdbuf = NULL;
  184. cs->cmdbytes = cs->curlen = 0;
  185. spin_unlock_irqrestore(&cs->cmdlock, flags);
  186. /* data queue */
  187. if (cs->bcs->tx_skb)
  188. dev_kfree_skb_any(cs->bcs->tx_skb);
  189. while ((skb = skb_dequeue(&cs->bcs->squeue)) != NULL)
  190. dev_kfree_skb_any(skb);
  191. }
  192. /* Gigaset Driver Interface */
  193. /* ======================== */
  194. /*
  195. * queue an AT command string for transmission to the Gigaset device
  196. * parameters:
  197. * cs controller state structure
  198. * buf buffer containing the string to send
  199. * len number of characters to send
  200. * wake_tasklet tasklet to run when transmission is complete, or NULL
  201. * return value:
  202. * number of bytes queued, or error code < 0
  203. */
  204. static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
  205. int len, struct tasklet_struct *wake_tasklet)
  206. {
  207. struct cmdbuf_t *cb;
  208. unsigned long flags;
  209. gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
  210. DEBUG_TRANSCMD : DEBUG_LOCKCMD,
  211. "CMD Transmit", len, buf);
  212. if (len <= 0)
  213. return 0;
  214. if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
  215. dev_err(cs->dev, "%s: out of memory!\n", __func__);
  216. return -ENOMEM;
  217. }
  218. memcpy(cb->buf, buf, len);
  219. cb->len = len;
  220. cb->offset = 0;
  221. cb->next = NULL;
  222. cb->wake_tasklet = wake_tasklet;
  223. spin_lock_irqsave(&cs->cmdlock, flags);
  224. cb->prev = cs->lastcmdbuf;
  225. if (cs->lastcmdbuf)
  226. cs->lastcmdbuf->next = cb;
  227. else {
  228. cs->cmdbuf = cb;
  229. cs->curlen = len;
  230. }
  231. cs->cmdbytes += len;
  232. cs->lastcmdbuf = cb;
  233. spin_unlock_irqrestore(&cs->cmdlock, flags);
  234. spin_lock_irqsave(&cs->lock, flags);
  235. if (cs->connected)
  236. tasklet_schedule(&cs->write_tasklet);
  237. spin_unlock_irqrestore(&cs->lock, flags);
  238. return len;
  239. }
  240. /*
  241. * tty_driver.write_room interface routine
  242. * return number of characters the driver will accept to be written
  243. * parameter:
  244. * controller state structure
  245. * return value:
  246. * number of characters
  247. */
  248. static int gigaset_write_room(struct cardstate *cs)
  249. {
  250. unsigned bytes;
  251. bytes = cs->cmdbytes;
  252. return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
  253. }
  254. /*
  255. * tty_driver.chars_in_buffer interface routine
  256. * return number of characters waiting to be sent
  257. * parameter:
  258. * controller state structure
  259. * return value:
  260. * number of characters
  261. */
  262. static int gigaset_chars_in_buffer(struct cardstate *cs)
  263. {
  264. return cs->cmdbytes;
  265. }
  266. /*
  267. * implementation of ioctl(GIGASET_BRKCHARS)
  268. * parameter:
  269. * controller state structure
  270. * return value:
  271. * -EINVAL (unimplemented function)
  272. */
  273. static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
  274. {
  275. /* not implemented */
  276. return -EINVAL;
  277. }
  278. /*
  279. * Open B channel
  280. * Called by "do_action" in ev-layer.c
  281. */
  282. static int gigaset_init_bchannel(struct bc_state *bcs)
  283. {
  284. /* nothing to do for M10x */
  285. gigaset_bchannel_up(bcs);
  286. return 0;
  287. }
  288. /*
  289. * Close B channel
  290. * Called by "do_action" in ev-layer.c
  291. */
  292. static int gigaset_close_bchannel(struct bc_state *bcs)
  293. {
  294. /* nothing to do for M10x */
  295. gigaset_bchannel_down(bcs);
  296. return 0;
  297. }
  298. /*
  299. * Set up B channel structure
  300. * This is called by "gigaset_initcs" in common.c
  301. */
  302. static int gigaset_initbcshw(struct bc_state *bcs)
  303. {
  304. /* unused */
  305. bcs->hw.ser = NULL;
  306. return 1;
  307. }
  308. /*
  309. * Free B channel structure
  310. * Called by "gigaset_freebcs" in common.c
  311. */
  312. static int gigaset_freebcshw(struct bc_state *bcs)
  313. {
  314. /* unused */
  315. return 1;
  316. }
  317. /*
  318. * Reinitialize B channel structure
  319. * This is called by "bcs_reinit" in common.c
  320. */
  321. static void gigaset_reinitbcshw(struct bc_state *bcs)
  322. {
  323. /* nothing to do for M10x */
  324. }
  325. /*
  326. * Free hardware specific device data
  327. * This will be called by "gigaset_freecs" in common.c
  328. */
  329. static void gigaset_freecshw(struct cardstate *cs)
  330. {
  331. tasklet_kill(&cs->write_tasklet);
  332. if (!cs->hw.ser)
  333. return;
  334. dev_set_drvdata(&cs->hw.ser->dev.dev, NULL);
  335. platform_device_unregister(&cs->hw.ser->dev);
  336. kfree(cs->hw.ser);
  337. cs->hw.ser = NULL;
  338. }
  339. static void gigaset_device_release(struct device *dev)
  340. {
  341. struct platform_device *pdev =
  342. container_of(dev, struct 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. err("%s: out of memory!", __func__);
  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. err("error %d registering platform device", 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->driver->tiocmset)
  386. return -EFAULT;
  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->driver->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. mutex_unlock(&cs->hw.ser->dead_mutex);
  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. info(DRIVER_AUTHOR);
  448. info(DRIVER_DESC);
  449. if (!driver) {
  450. err("%s: no driver structure", __func__);
  451. return -ENODEV;
  452. }
  453. /* allocate memory for our device state and intialize it */
  454. if (!(cs = gigaset_initcs(driver, 1, 1, 0, cidmode,
  455. GIGASET_MODULENAME)))
  456. goto error;
  457. cs->dev = &cs->hw.ser->dev.dev;
  458. cs->hw.ser->tty = tty;
  459. mutex_init(&cs->hw.ser->dead_mutex);
  460. atomic_set(&cs->hw.ser->refcnt, 1);
  461. tty->disc_data = cs;
  462. /* OK.. Initialization of the datastructures and the HW is done.. Now
  463. * startup system and notify the LL that we are ready to run
  464. */
  465. if (startmode == SM_LOCKED)
  466. atomic_set(&cs->mstate, MS_LOCKED);
  467. if (!gigaset_start(cs)) {
  468. tasklet_kill(&cs->write_tasklet);
  469. goto error;
  470. }
  471. gig_dbg(DEBUG_INIT, "Startup of HLL done");
  472. mutex_lock(&cs->hw.ser->dead_mutex);
  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. mutex_lock(&cs->hw.ser->dead_mutex);
  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 = atomic_read(&inbuf->tail);
  622. head = atomic_read(&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. atomic_set(&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);