uart.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /*
  2. * UART driver for the Greybus "generic" UART module.
  3. *
  4. * Copyright 2014 Google Inc.
  5. * Copyright 2014 Linaro Ltd.
  6. *
  7. * Released under the GPLv2 only.
  8. *
  9. * Heavily based on drivers/usb/class/cdc-acm.c and
  10. * drivers/usb/serial/usb-serial.c.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/module.h>
  16. #include <linux/sched.h>
  17. #include <linux/wait.h>
  18. #include <linux/slab.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/mutex.h>
  21. #include <linux/tty.h>
  22. #include <linux/serial.h>
  23. #include <linux/tty_driver.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/serial.h>
  26. #include <linux/idr.h>
  27. #include <linux/fs.h>
  28. #include <linux/kdev_t.h>
  29. #include <linux/kfifo.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/completion.h>
  32. #include "greybus.h"
  33. #include "gbphy.h"
  34. #define GB_NUM_MINORS 16 /* 16 is is more than enough */
  35. #define GB_NAME "ttyGB"
  36. #define GB_UART_WRITE_FIFO_SIZE PAGE_SIZE
  37. #define GB_UART_WRITE_ROOM_MARGIN 1 /* leave some space in fifo */
  38. #define GB_UART_FIRMWARE_CREDITS 4096
  39. #define GB_UART_CREDIT_WAIT_TIMEOUT_MSEC 10000
  40. struct gb_tty_line_coding {
  41. __le32 rate;
  42. __u8 format;
  43. __u8 parity;
  44. __u8 data_bits;
  45. __u8 flow_control;
  46. };
  47. struct gb_tty {
  48. struct gbphy_device *gbphy_dev;
  49. struct tty_port port;
  50. void *buffer;
  51. size_t buffer_payload_max;
  52. struct gb_connection *connection;
  53. u16 cport_id;
  54. unsigned int minor;
  55. unsigned char clocal;
  56. bool disconnected;
  57. spinlock_t read_lock;
  58. spinlock_t write_lock;
  59. struct async_icount iocount;
  60. struct async_icount oldcount;
  61. wait_queue_head_t wioctl;
  62. struct mutex mutex;
  63. u8 ctrlin; /* input control lines */
  64. u8 ctrlout; /* output control lines */
  65. struct gb_tty_line_coding line_coding;
  66. struct work_struct tx_work;
  67. struct kfifo write_fifo;
  68. bool close_pending;
  69. unsigned int credits;
  70. struct completion credits_complete;
  71. };
  72. static struct tty_driver *gb_tty_driver;
  73. static DEFINE_IDR(tty_minors);
  74. static DEFINE_MUTEX(table_lock);
  75. static int gb_uart_receive_data_handler(struct gb_operation *op)
  76. {
  77. struct gb_connection *connection = op->connection;
  78. struct gb_tty *gb_tty = gb_connection_get_data(connection);
  79. struct tty_port *port = &gb_tty->port;
  80. struct gb_message *request = op->request;
  81. struct gb_uart_recv_data_request *receive_data;
  82. u16 recv_data_size;
  83. int count;
  84. unsigned long tty_flags = TTY_NORMAL;
  85. if (request->payload_size < sizeof(*receive_data)) {
  86. dev_err(&gb_tty->gbphy_dev->dev,
  87. "short receive-data request received (%zu < %zu)\n",
  88. request->payload_size, sizeof(*receive_data));
  89. return -EINVAL;
  90. }
  91. receive_data = op->request->payload;
  92. recv_data_size = le16_to_cpu(receive_data->size);
  93. if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
  94. dev_err(&gb_tty->gbphy_dev->dev,
  95. "malformed receive-data request received (%u != %zu)\n",
  96. recv_data_size,
  97. request->payload_size - sizeof(*receive_data));
  98. return -EINVAL;
  99. }
  100. if (!recv_data_size)
  101. return -EINVAL;
  102. if (receive_data->flags) {
  103. if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
  104. tty_flags = TTY_BREAK;
  105. else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
  106. tty_flags = TTY_PARITY;
  107. else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
  108. tty_flags = TTY_FRAME;
  109. /* overrun is special, not associated with a char */
  110. if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
  111. tty_insert_flip_char(port, 0, TTY_OVERRUN);
  112. }
  113. count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
  114. tty_flags, recv_data_size);
  115. if (count != recv_data_size) {
  116. dev_err(&gb_tty->gbphy_dev->dev,
  117. "UART: RX 0x%08x bytes only wrote 0x%08x\n",
  118. recv_data_size, count);
  119. }
  120. if (count)
  121. tty_flip_buffer_push(port);
  122. return 0;
  123. }
  124. static int gb_uart_serial_state_handler(struct gb_operation *op)
  125. {
  126. struct gb_connection *connection = op->connection;
  127. struct gb_tty *gb_tty = gb_connection_get_data(connection);
  128. struct gb_message *request = op->request;
  129. struct gb_uart_serial_state_request *serial_state;
  130. if (request->payload_size < sizeof(*serial_state)) {
  131. dev_err(&gb_tty->gbphy_dev->dev,
  132. "short serial-state event received (%zu < %zu)\n",
  133. request->payload_size, sizeof(*serial_state));
  134. return -EINVAL;
  135. }
  136. serial_state = request->payload;
  137. gb_tty->ctrlin = serial_state->control;
  138. return 0;
  139. }
  140. static int gb_uart_receive_credits_handler(struct gb_operation *op)
  141. {
  142. struct gb_connection *connection = op->connection;
  143. struct gb_tty *gb_tty = gb_connection_get_data(connection);
  144. struct gb_message *request = op->request;
  145. struct gb_uart_receive_credits_request *credit_request;
  146. unsigned long flags;
  147. unsigned int incoming_credits;
  148. int ret = 0;
  149. if (request->payload_size < sizeof(*credit_request)) {
  150. dev_err(&gb_tty->gbphy_dev->dev,
  151. "short receive_credits event received (%zu < %zu)\n",
  152. request->payload_size,
  153. sizeof(*credit_request));
  154. return -EINVAL;
  155. }
  156. credit_request = request->payload;
  157. incoming_credits = le16_to_cpu(credit_request->count);
  158. spin_lock_irqsave(&gb_tty->write_lock, flags);
  159. gb_tty->credits += incoming_credits;
  160. if (gb_tty->credits > GB_UART_FIRMWARE_CREDITS) {
  161. gb_tty->credits -= incoming_credits;
  162. ret = -EINVAL;
  163. }
  164. spin_unlock_irqrestore(&gb_tty->write_lock, flags);
  165. if (ret) {
  166. dev_err(&gb_tty->gbphy_dev->dev,
  167. "invalid number of incoming credits: %d\n",
  168. incoming_credits);
  169. return ret;
  170. }
  171. if (!gb_tty->close_pending)
  172. schedule_work(&gb_tty->tx_work);
  173. /*
  174. * the port the tty layer may be waiting for credits
  175. */
  176. tty_port_tty_wakeup(&gb_tty->port);
  177. if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
  178. complete(&gb_tty->credits_complete);
  179. return ret;
  180. }
  181. static int gb_uart_request_handler(struct gb_operation *op)
  182. {
  183. struct gb_connection *connection = op->connection;
  184. struct gb_tty *gb_tty = gb_connection_get_data(connection);
  185. int type = op->type;
  186. int ret;
  187. switch (type) {
  188. case GB_UART_TYPE_RECEIVE_DATA:
  189. ret = gb_uart_receive_data_handler(op);
  190. break;
  191. case GB_UART_TYPE_SERIAL_STATE:
  192. ret = gb_uart_serial_state_handler(op);
  193. break;
  194. case GB_UART_TYPE_RECEIVE_CREDITS:
  195. ret = gb_uart_receive_credits_handler(op);
  196. break;
  197. default:
  198. dev_err(&gb_tty->gbphy_dev->dev,
  199. "unsupported unsolicited request: 0x%02x\n", type);
  200. ret = -EINVAL;
  201. }
  202. return ret;
  203. }
  204. static void gb_uart_tx_write_work(struct work_struct *work)
  205. {
  206. struct gb_uart_send_data_request *request;
  207. struct gb_tty *gb_tty;
  208. unsigned long flags;
  209. unsigned int send_size;
  210. int ret;
  211. gb_tty = container_of(work, struct gb_tty, tx_work);
  212. request = gb_tty->buffer;
  213. while (1) {
  214. if (gb_tty->close_pending)
  215. break;
  216. spin_lock_irqsave(&gb_tty->write_lock, flags);
  217. send_size = gb_tty->buffer_payload_max;
  218. if (send_size > gb_tty->credits)
  219. send_size = gb_tty->credits;
  220. send_size = kfifo_out_peek(&gb_tty->write_fifo,
  221. &request->data[0],
  222. send_size);
  223. if (!send_size) {
  224. spin_unlock_irqrestore(&gb_tty->write_lock, flags);
  225. break;
  226. }
  227. gb_tty->credits -= send_size;
  228. spin_unlock_irqrestore(&gb_tty->write_lock, flags);
  229. request->size = cpu_to_le16(send_size);
  230. ret = gb_operation_sync(gb_tty->connection,
  231. GB_UART_TYPE_SEND_DATA,
  232. request, sizeof(*request) + send_size,
  233. NULL, 0);
  234. if (ret) {
  235. dev_err(&gb_tty->gbphy_dev->dev,
  236. "send data error: %d\n", ret);
  237. spin_lock_irqsave(&gb_tty->write_lock, flags);
  238. gb_tty->credits += send_size;
  239. spin_unlock_irqrestore(&gb_tty->write_lock, flags);
  240. if (!gb_tty->close_pending)
  241. schedule_work(work);
  242. return;
  243. }
  244. spin_lock_irqsave(&gb_tty->write_lock, flags);
  245. ret = kfifo_out(&gb_tty->write_fifo, &request->data[0],
  246. send_size);
  247. spin_unlock_irqrestore(&gb_tty->write_lock, flags);
  248. tty_port_tty_wakeup(&gb_tty->port);
  249. }
  250. }
  251. static int send_line_coding(struct gb_tty *tty)
  252. {
  253. struct gb_uart_set_line_coding_request request;
  254. memcpy(&request, &tty->line_coding,
  255. sizeof(tty->line_coding));
  256. return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
  257. &request, sizeof(request), NULL, 0);
  258. }
  259. static int send_control(struct gb_tty *gb_tty, u8 control)
  260. {
  261. struct gb_uart_set_control_line_state_request request;
  262. request.control = control;
  263. return gb_operation_sync(gb_tty->connection,
  264. GB_UART_TYPE_SET_CONTROL_LINE_STATE,
  265. &request, sizeof(request), NULL, 0);
  266. }
  267. static int send_break(struct gb_tty *gb_tty, u8 state)
  268. {
  269. struct gb_uart_set_break_request request;
  270. if ((state != 0) && (state != 1)) {
  271. dev_err(&gb_tty->gbphy_dev->dev,
  272. "invalid break state of %d\n", state);
  273. return -EINVAL;
  274. }
  275. request.state = state;
  276. return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
  277. &request, sizeof(request), NULL, 0);
  278. }
  279. static int gb_uart_wait_for_all_credits(struct gb_tty *gb_tty)
  280. {
  281. int ret;
  282. if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
  283. return 0;
  284. ret = wait_for_completion_timeout(&gb_tty->credits_complete,
  285. msecs_to_jiffies(GB_UART_CREDIT_WAIT_TIMEOUT_MSEC));
  286. if (!ret) {
  287. dev_err(&gb_tty->gbphy_dev->dev,
  288. "time out waiting for credits\n");
  289. return -ETIMEDOUT;
  290. }
  291. return 0;
  292. }
  293. static int gb_uart_flush(struct gb_tty *gb_tty, u8 flags)
  294. {
  295. struct gb_uart_serial_flush_request request;
  296. request.flags = flags;
  297. return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_FLUSH_FIFOS,
  298. &request, sizeof(request), NULL, 0);
  299. }
  300. static struct gb_tty *get_gb_by_minor(unsigned int minor)
  301. {
  302. struct gb_tty *gb_tty;
  303. mutex_lock(&table_lock);
  304. gb_tty = idr_find(&tty_minors, minor);
  305. if (gb_tty) {
  306. mutex_lock(&gb_tty->mutex);
  307. if (gb_tty->disconnected) {
  308. mutex_unlock(&gb_tty->mutex);
  309. gb_tty = NULL;
  310. } else {
  311. tty_port_get(&gb_tty->port);
  312. mutex_unlock(&gb_tty->mutex);
  313. }
  314. }
  315. mutex_unlock(&table_lock);
  316. return gb_tty;
  317. }
  318. static int alloc_minor(struct gb_tty *gb_tty)
  319. {
  320. int minor;
  321. mutex_lock(&table_lock);
  322. minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
  323. mutex_unlock(&table_lock);
  324. if (minor >= 0)
  325. gb_tty->minor = minor;
  326. return minor;
  327. }
  328. static void release_minor(struct gb_tty *gb_tty)
  329. {
  330. int minor = gb_tty->minor;
  331. gb_tty->minor = 0; /* Maybe should use an invalid value instead */
  332. mutex_lock(&table_lock);
  333. idr_remove(&tty_minors, minor);
  334. mutex_unlock(&table_lock);
  335. }
  336. static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  337. {
  338. struct gb_tty *gb_tty;
  339. int retval;
  340. gb_tty = get_gb_by_minor(tty->index);
  341. if (!gb_tty)
  342. return -ENODEV;
  343. retval = tty_standard_install(driver, tty);
  344. if (retval)
  345. goto error;
  346. tty->driver_data = gb_tty;
  347. return 0;
  348. error:
  349. tty_port_put(&gb_tty->port);
  350. return retval;
  351. }
  352. static int gb_tty_open(struct tty_struct *tty, struct file *file)
  353. {
  354. struct gb_tty *gb_tty = tty->driver_data;
  355. return tty_port_open(&gb_tty->port, tty, file);
  356. }
  357. static void gb_tty_close(struct tty_struct *tty, struct file *file)
  358. {
  359. struct gb_tty *gb_tty = tty->driver_data;
  360. tty_port_close(&gb_tty->port, tty, file);
  361. }
  362. static void gb_tty_cleanup(struct tty_struct *tty)
  363. {
  364. struct gb_tty *gb_tty = tty->driver_data;
  365. tty_port_put(&gb_tty->port);
  366. }
  367. static void gb_tty_hangup(struct tty_struct *tty)
  368. {
  369. struct gb_tty *gb_tty = tty->driver_data;
  370. tty_port_hangup(&gb_tty->port);
  371. }
  372. static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
  373. int count)
  374. {
  375. struct gb_tty *gb_tty = tty->driver_data;
  376. count = kfifo_in_spinlocked(&gb_tty->write_fifo, buf, count,
  377. &gb_tty->write_lock);
  378. if (count && !gb_tty->close_pending)
  379. schedule_work(&gb_tty->tx_work);
  380. return count;
  381. }
  382. static int gb_tty_write_room(struct tty_struct *tty)
  383. {
  384. struct gb_tty *gb_tty = tty->driver_data;
  385. unsigned long flags;
  386. int room;
  387. spin_lock_irqsave(&gb_tty->write_lock, flags);
  388. room = kfifo_avail(&gb_tty->write_fifo);
  389. spin_unlock_irqrestore(&gb_tty->write_lock, flags);
  390. room -= GB_UART_WRITE_ROOM_MARGIN;
  391. if (room < 0)
  392. return 0;
  393. return room;
  394. }
  395. static int gb_tty_chars_in_buffer(struct tty_struct *tty)
  396. {
  397. struct gb_tty *gb_tty = tty->driver_data;
  398. unsigned long flags;
  399. int chars;
  400. spin_lock_irqsave(&gb_tty->write_lock, flags);
  401. chars = kfifo_len(&gb_tty->write_fifo);
  402. if (gb_tty->credits < GB_UART_FIRMWARE_CREDITS)
  403. chars += GB_UART_FIRMWARE_CREDITS - gb_tty->credits;
  404. spin_unlock_irqrestore(&gb_tty->write_lock, flags);
  405. return chars;
  406. }
  407. static int gb_tty_break_ctl(struct tty_struct *tty, int state)
  408. {
  409. struct gb_tty *gb_tty = tty->driver_data;
  410. return send_break(gb_tty, state ? 1 : 0);
  411. }
  412. static void gb_tty_set_termios(struct tty_struct *tty,
  413. struct ktermios *termios_old)
  414. {
  415. struct gb_tty *gb_tty = tty->driver_data;
  416. struct ktermios *termios = &tty->termios;
  417. struct gb_tty_line_coding newline;
  418. u8 newctrl = gb_tty->ctrlout;
  419. newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
  420. newline.format = termios->c_cflag & CSTOPB ?
  421. GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
  422. newline.parity = termios->c_cflag & PARENB ?
  423. (termios->c_cflag & PARODD ? 1 : 2) +
  424. (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  425. switch (termios->c_cflag & CSIZE) {
  426. case CS5:
  427. newline.data_bits = 5;
  428. break;
  429. case CS6:
  430. newline.data_bits = 6;
  431. break;
  432. case CS7:
  433. newline.data_bits = 7;
  434. break;
  435. case CS8:
  436. default:
  437. newline.data_bits = 8;
  438. break;
  439. }
  440. /* FIXME: needs to clear unsupported bits in the termios */
  441. gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
  442. if (C_BAUD(tty) == B0) {
  443. newline.rate = gb_tty->line_coding.rate;
  444. newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
  445. } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
  446. newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
  447. }
  448. if (newctrl != gb_tty->ctrlout) {
  449. gb_tty->ctrlout = newctrl;
  450. send_control(gb_tty, newctrl);
  451. }
  452. if (C_CRTSCTS(tty) && C_BAUD(tty) != B0)
  453. newline.flow_control |= GB_SERIAL_AUTO_RTSCTS_EN;
  454. else
  455. newline.flow_control &= ~GB_SERIAL_AUTO_RTSCTS_EN;
  456. if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
  457. memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
  458. send_line_coding(gb_tty);
  459. }
  460. }
  461. static int gb_tty_tiocmget(struct tty_struct *tty)
  462. {
  463. struct gb_tty *gb_tty = tty->driver_data;
  464. return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
  465. (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
  466. (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
  467. (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
  468. (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
  469. TIOCM_CTS;
  470. }
  471. static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
  472. unsigned int clear)
  473. {
  474. struct gb_tty *gb_tty = tty->driver_data;
  475. u8 newctrl = gb_tty->ctrlout;
  476. set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
  477. (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
  478. clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
  479. (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
  480. newctrl = (newctrl & ~clear) | set;
  481. if (gb_tty->ctrlout == newctrl)
  482. return 0;
  483. gb_tty->ctrlout = newctrl;
  484. return send_control(gb_tty, newctrl);
  485. }
  486. static void gb_tty_throttle(struct tty_struct *tty)
  487. {
  488. struct gb_tty *gb_tty = tty->driver_data;
  489. unsigned char stop_char;
  490. int retval;
  491. if (I_IXOFF(tty)) {
  492. stop_char = STOP_CHAR(tty);
  493. retval = gb_tty_write(tty, &stop_char, 1);
  494. if (retval <= 0)
  495. return;
  496. }
  497. if (tty->termios.c_cflag & CRTSCTS) {
  498. gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
  499. retval = send_control(gb_tty, gb_tty->ctrlout);
  500. }
  501. }
  502. static void gb_tty_unthrottle(struct tty_struct *tty)
  503. {
  504. struct gb_tty *gb_tty = tty->driver_data;
  505. unsigned char start_char;
  506. int retval;
  507. if (I_IXOFF(tty)) {
  508. start_char = START_CHAR(tty);
  509. retval = gb_tty_write(tty, &start_char, 1);
  510. if (retval <= 0)
  511. return;
  512. }
  513. if (tty->termios.c_cflag & CRTSCTS) {
  514. gb_tty->ctrlout |= GB_UART_CTRL_RTS;
  515. retval = send_control(gb_tty, gb_tty->ctrlout);
  516. }
  517. }
  518. static int get_serial_info(struct gb_tty *gb_tty,
  519. struct serial_struct __user *info)
  520. {
  521. struct serial_struct tmp;
  522. if (!info)
  523. return -EINVAL;
  524. memset(&tmp, 0, sizeof(tmp));
  525. tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
  526. tmp.type = PORT_16550A;
  527. tmp.line = gb_tty->minor;
  528. tmp.xmit_fifo_size = 16;
  529. tmp.baud_base = 9600;
  530. tmp.close_delay = gb_tty->port.close_delay / 10;
  531. tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  532. ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
  533. if (copy_to_user(info, &tmp, sizeof(tmp)))
  534. return -EFAULT;
  535. return 0;
  536. }
  537. static int set_serial_info(struct gb_tty *gb_tty,
  538. struct serial_struct __user *newinfo)
  539. {
  540. struct serial_struct new_serial;
  541. unsigned int closing_wait;
  542. unsigned int close_delay;
  543. int retval = 0;
  544. if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
  545. return -EFAULT;
  546. close_delay = new_serial.close_delay * 10;
  547. closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  548. ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
  549. mutex_lock(&gb_tty->port.mutex);
  550. if (!capable(CAP_SYS_ADMIN)) {
  551. if ((close_delay != gb_tty->port.close_delay) ||
  552. (closing_wait != gb_tty->port.closing_wait))
  553. retval = -EPERM;
  554. else
  555. retval = -EOPNOTSUPP;
  556. } else {
  557. gb_tty->port.close_delay = close_delay;
  558. gb_tty->port.closing_wait = closing_wait;
  559. }
  560. mutex_unlock(&gb_tty->port.mutex);
  561. return retval;
  562. }
  563. static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
  564. {
  565. int retval = 0;
  566. DECLARE_WAITQUEUE(wait, current);
  567. struct async_icount old;
  568. struct async_icount new;
  569. if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
  570. return -EINVAL;
  571. do {
  572. spin_lock_irq(&gb_tty->read_lock);
  573. old = gb_tty->oldcount;
  574. new = gb_tty->iocount;
  575. gb_tty->oldcount = new;
  576. spin_unlock_irq(&gb_tty->read_lock);
  577. if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
  578. break;
  579. if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
  580. break;
  581. if ((arg & TIOCM_RI) && (old.rng != new.rng))
  582. break;
  583. add_wait_queue(&gb_tty->wioctl, &wait);
  584. set_current_state(TASK_INTERRUPTIBLE);
  585. schedule();
  586. remove_wait_queue(&gb_tty->wioctl, &wait);
  587. if (gb_tty->disconnected) {
  588. if (arg & TIOCM_CD)
  589. break;
  590. retval = -ENODEV;
  591. } else if (signal_pending(current)) {
  592. retval = -ERESTARTSYS;
  593. }
  594. } while (!retval);
  595. return retval;
  596. }
  597. static int get_serial_usage(struct gb_tty *gb_tty,
  598. struct serial_icounter_struct __user *count)
  599. {
  600. struct serial_icounter_struct icount;
  601. int retval = 0;
  602. memset(&icount, 0, sizeof(icount));
  603. icount.dsr = gb_tty->iocount.dsr;
  604. icount.rng = gb_tty->iocount.rng;
  605. icount.dcd = gb_tty->iocount.dcd;
  606. icount.frame = gb_tty->iocount.frame;
  607. icount.overrun = gb_tty->iocount.overrun;
  608. icount.parity = gb_tty->iocount.parity;
  609. icount.brk = gb_tty->iocount.brk;
  610. if (copy_to_user(count, &icount, sizeof(icount)) > 0)
  611. retval = -EFAULT;
  612. return retval;
  613. }
  614. static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
  615. unsigned long arg)
  616. {
  617. struct gb_tty *gb_tty = tty->driver_data;
  618. switch (cmd) {
  619. case TIOCGSERIAL:
  620. return get_serial_info(gb_tty,
  621. (struct serial_struct __user *)arg);
  622. case TIOCSSERIAL:
  623. return set_serial_info(gb_tty,
  624. (struct serial_struct __user *)arg);
  625. case TIOCMIWAIT:
  626. return wait_serial_change(gb_tty, arg);
  627. case TIOCGICOUNT:
  628. return get_serial_usage(gb_tty,
  629. (struct serial_icounter_struct __user *)arg);
  630. }
  631. return -ENOIOCTLCMD;
  632. }
  633. static void gb_tty_dtr_rts(struct tty_port *port, int on)
  634. {
  635. struct gb_tty *gb_tty;
  636. u8 newctrl;
  637. gb_tty = container_of(port, struct gb_tty, port);
  638. newctrl = gb_tty->ctrlout;
  639. if (on)
  640. newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
  641. else
  642. newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
  643. gb_tty->ctrlout = newctrl;
  644. send_control(gb_tty, newctrl);
  645. }
  646. static int gb_tty_port_activate(struct tty_port *port,
  647. struct tty_struct *tty)
  648. {
  649. struct gb_tty *gb_tty;
  650. gb_tty = container_of(port, struct gb_tty, port);
  651. return gbphy_runtime_get_sync(gb_tty->gbphy_dev);
  652. }
  653. static void gb_tty_port_shutdown(struct tty_port *port)
  654. {
  655. struct gb_tty *gb_tty;
  656. unsigned long flags;
  657. int ret;
  658. gb_tty = container_of(port, struct gb_tty, port);
  659. gb_tty->close_pending = true;
  660. cancel_work_sync(&gb_tty->tx_work);
  661. spin_lock_irqsave(&gb_tty->write_lock, flags);
  662. kfifo_reset_out(&gb_tty->write_fifo);
  663. spin_unlock_irqrestore(&gb_tty->write_lock, flags);
  664. if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
  665. goto out;
  666. ret = gb_uart_flush(gb_tty, GB_SERIAL_FLAG_FLUSH_TRANSMITTER);
  667. if (ret) {
  668. dev_err(&gb_tty->gbphy_dev->dev,
  669. "error flushing transmitter: %d\n", ret);
  670. }
  671. gb_uart_wait_for_all_credits(gb_tty);
  672. out:
  673. gb_tty->close_pending = false;
  674. gbphy_runtime_put_autosuspend(gb_tty->gbphy_dev);
  675. }
  676. static const struct tty_operations gb_ops = {
  677. .install = gb_tty_install,
  678. .open = gb_tty_open,
  679. .close = gb_tty_close,
  680. .cleanup = gb_tty_cleanup,
  681. .hangup = gb_tty_hangup,
  682. .write = gb_tty_write,
  683. .write_room = gb_tty_write_room,
  684. .ioctl = gb_tty_ioctl,
  685. .throttle = gb_tty_throttle,
  686. .unthrottle = gb_tty_unthrottle,
  687. .chars_in_buffer = gb_tty_chars_in_buffer,
  688. .break_ctl = gb_tty_break_ctl,
  689. .set_termios = gb_tty_set_termios,
  690. .tiocmget = gb_tty_tiocmget,
  691. .tiocmset = gb_tty_tiocmset,
  692. };
  693. static struct tty_port_operations gb_port_ops = {
  694. .dtr_rts = gb_tty_dtr_rts,
  695. .activate = gb_tty_port_activate,
  696. .shutdown = gb_tty_port_shutdown,
  697. };
  698. static int gb_uart_probe(struct gbphy_device *gbphy_dev,
  699. const struct gbphy_device_id *id)
  700. {
  701. struct gb_connection *connection;
  702. size_t max_payload;
  703. struct gb_tty *gb_tty;
  704. struct device *tty_dev;
  705. int retval;
  706. int minor;
  707. gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
  708. if (!gb_tty)
  709. return -ENOMEM;
  710. connection = gb_connection_create(gbphy_dev->bundle,
  711. le16_to_cpu(gbphy_dev->cport_desc->id),
  712. gb_uart_request_handler);
  713. if (IS_ERR(connection)) {
  714. retval = PTR_ERR(connection);
  715. goto exit_tty_free;
  716. }
  717. max_payload = gb_operation_get_payload_size_max(connection);
  718. if (max_payload < sizeof(struct gb_uart_send_data_request)) {
  719. retval = -EINVAL;
  720. goto exit_connection_destroy;
  721. }
  722. gb_tty->buffer_payload_max = max_payload -
  723. sizeof(struct gb_uart_send_data_request);
  724. gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
  725. if (!gb_tty->buffer) {
  726. retval = -ENOMEM;
  727. goto exit_connection_destroy;
  728. }
  729. INIT_WORK(&gb_tty->tx_work, gb_uart_tx_write_work);
  730. retval = kfifo_alloc(&gb_tty->write_fifo, GB_UART_WRITE_FIFO_SIZE,
  731. GFP_KERNEL);
  732. if (retval)
  733. goto exit_buf_free;
  734. gb_tty->credits = GB_UART_FIRMWARE_CREDITS;
  735. init_completion(&gb_tty->credits_complete);
  736. minor = alloc_minor(gb_tty);
  737. if (minor < 0) {
  738. if (minor == -ENOSPC) {
  739. dev_err(&gbphy_dev->dev,
  740. "no more free minor numbers\n");
  741. retval = -ENODEV;
  742. } else {
  743. retval = minor;
  744. }
  745. goto exit_kfifo_free;
  746. }
  747. gb_tty->minor = minor;
  748. spin_lock_init(&gb_tty->write_lock);
  749. spin_lock_init(&gb_tty->read_lock);
  750. init_waitqueue_head(&gb_tty->wioctl);
  751. mutex_init(&gb_tty->mutex);
  752. tty_port_init(&gb_tty->port);
  753. gb_tty->port.ops = &gb_port_ops;
  754. gb_tty->connection = connection;
  755. gb_tty->gbphy_dev = gbphy_dev;
  756. gb_connection_set_data(connection, gb_tty);
  757. gb_gbphy_set_data(gbphy_dev, gb_tty);
  758. retval = gb_connection_enable_tx(connection);
  759. if (retval)
  760. goto exit_release_minor;
  761. send_control(gb_tty, gb_tty->ctrlout);
  762. /* initialize the uart to be 9600n81 */
  763. gb_tty->line_coding.rate = cpu_to_le32(9600);
  764. gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
  765. gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
  766. gb_tty->line_coding.data_bits = 8;
  767. send_line_coding(gb_tty);
  768. retval = gb_connection_enable(connection);
  769. if (retval)
  770. goto exit_connection_disable;
  771. tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
  772. &gbphy_dev->dev);
  773. if (IS_ERR(tty_dev)) {
  774. retval = PTR_ERR(tty_dev);
  775. goto exit_connection_disable;
  776. }
  777. gbphy_runtime_put_autosuspend(gbphy_dev);
  778. return 0;
  779. exit_connection_disable:
  780. gb_connection_disable(connection);
  781. exit_release_minor:
  782. release_minor(gb_tty);
  783. exit_kfifo_free:
  784. kfifo_free(&gb_tty->write_fifo);
  785. exit_buf_free:
  786. kfree(gb_tty->buffer);
  787. exit_connection_destroy:
  788. gb_connection_destroy(connection);
  789. exit_tty_free:
  790. kfree(gb_tty);
  791. return retval;
  792. }
  793. static void gb_uart_remove(struct gbphy_device *gbphy_dev)
  794. {
  795. struct gb_tty *gb_tty = gb_gbphy_get_data(gbphy_dev);
  796. struct gb_connection *connection = gb_tty->connection;
  797. struct tty_struct *tty;
  798. int ret;
  799. ret = gbphy_runtime_get_sync(gbphy_dev);
  800. if (ret)
  801. gbphy_runtime_get_noresume(gbphy_dev);
  802. mutex_lock(&gb_tty->mutex);
  803. gb_tty->disconnected = true;
  804. wake_up_all(&gb_tty->wioctl);
  805. mutex_unlock(&gb_tty->mutex);
  806. tty = tty_port_tty_get(&gb_tty->port);
  807. if (tty) {
  808. tty_vhangup(tty);
  809. tty_kref_put(tty);
  810. }
  811. gb_connection_disable_rx(connection);
  812. tty_unregister_device(gb_tty_driver, gb_tty->minor);
  813. /* FIXME - free transmit / receive buffers */
  814. gb_connection_disable(connection);
  815. tty_port_destroy(&gb_tty->port);
  816. gb_connection_destroy(connection);
  817. release_minor(gb_tty);
  818. kfifo_free(&gb_tty->write_fifo);
  819. kfree(gb_tty->buffer);
  820. kfree(gb_tty);
  821. }
  822. static int gb_tty_init(void)
  823. {
  824. int retval = 0;
  825. gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
  826. if (IS_ERR(gb_tty_driver)) {
  827. pr_err("Can not allocate tty driver\n");
  828. retval = -ENOMEM;
  829. goto fail_unregister_dev;
  830. }
  831. gb_tty_driver->driver_name = "gb";
  832. gb_tty_driver->name = GB_NAME;
  833. gb_tty_driver->major = 0;
  834. gb_tty_driver->minor_start = 0;
  835. gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  836. gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  837. gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  838. gb_tty_driver->init_termios = tty_std_termios;
  839. gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  840. tty_set_operations(gb_tty_driver, &gb_ops);
  841. retval = tty_register_driver(gb_tty_driver);
  842. if (retval) {
  843. pr_err("Can not register tty driver: %d\n", retval);
  844. goto fail_put_gb_tty;
  845. }
  846. return 0;
  847. fail_put_gb_tty:
  848. put_tty_driver(gb_tty_driver);
  849. fail_unregister_dev:
  850. return retval;
  851. }
  852. static void gb_tty_exit(void)
  853. {
  854. tty_unregister_driver(gb_tty_driver);
  855. put_tty_driver(gb_tty_driver);
  856. idr_destroy(&tty_minors);
  857. }
  858. static const struct gbphy_device_id gb_uart_id_table[] = {
  859. { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_UART) },
  860. { },
  861. };
  862. MODULE_DEVICE_TABLE(gbphy, gb_uart_id_table);
  863. static struct gbphy_driver uart_driver = {
  864. .name = "uart",
  865. .probe = gb_uart_probe,
  866. .remove = gb_uart_remove,
  867. .id_table = gb_uart_id_table,
  868. };
  869. static int gb_uart_driver_init(void)
  870. {
  871. int ret;
  872. ret = gb_tty_init();
  873. if (ret)
  874. return ret;
  875. ret = gb_gbphy_register(&uart_driver);
  876. if (ret) {
  877. gb_tty_exit();
  878. return ret;
  879. }
  880. return 0;
  881. }
  882. module_init(gb_uart_driver_init);
  883. static void gb_uart_driver_exit(void)
  884. {
  885. gb_gbphy_deregister(&uart_driver);
  886. gb_tty_exit();
  887. }
  888. module_exit(gb_uart_driver_exit);
  889. MODULE_LICENSE("GPL v2");