vuart.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * PS3 virtual uart
  3. *
  4. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  5. * Copyright 2006 Sony Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <asm/ps3.h>
  24. #include <asm/lv1call.h>
  25. #include <asm/bitops.h>
  26. #include "vuart.h"
  27. MODULE_AUTHOR("Sony Corporation");
  28. MODULE_LICENSE("GPL v2");
  29. MODULE_DESCRIPTION("ps3 vuart");
  30. /**
  31. * vuart - An inter-partition data link service.
  32. * port 0: PS3 AV Settings.
  33. * port 2: PS3 System Manager.
  34. *
  35. * The vuart provides a bi-directional byte stream data link between logical
  36. * partitions. Its primary role is as a communications link between the guest
  37. * OS and the system policy module. The current HV does not support any
  38. * connections other than those listed.
  39. */
  40. enum {PORT_COUNT = 3,};
  41. enum vuart_param {
  42. PARAM_TX_TRIGGER = 0,
  43. PARAM_RX_TRIGGER = 1,
  44. PARAM_INTERRUPT_MASK = 2,
  45. PARAM_RX_BUF_SIZE = 3, /* read only */
  46. PARAM_RX_BYTES = 4, /* read only */
  47. PARAM_TX_BUF_SIZE = 5, /* read only */
  48. PARAM_TX_BYTES = 6, /* read only */
  49. PARAM_INTERRUPT_STATUS = 7, /* read only */
  50. };
  51. enum vuart_interrupt_bit {
  52. INTERRUPT_BIT_TX = 0,
  53. INTERRUPT_BIT_RX = 1,
  54. INTERRUPT_BIT_DISCONNECT = 2,
  55. };
  56. enum vuart_interrupt_mask {
  57. INTERRUPT_MASK_TX = 1,
  58. INTERRUPT_MASK_RX = 2,
  59. INTERRUPT_MASK_DISCONNECT = 4,
  60. };
  61. /**
  62. * struct ports_bmp - bitmap indicating ports needing service.
  63. *
  64. * A 256 bit read only bitmap indicating ports needing service. Do not write
  65. * to these bits. Must not cross a page boundary.
  66. */
  67. struct ports_bmp {
  68. u64 status;
  69. u64 unused[3];
  70. } __attribute__ ((aligned (32)));
  71. /* redefine dev_dbg to do a syntax check */
  72. #if !defined(DEBUG)
  73. #undef dev_dbg
  74. static inline int __attribute__ ((format (printf, 2, 3))) dev_dbg(
  75. const struct device *_dev, const char *fmt, ...) {return 0;}
  76. #endif
  77. #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
  78. static void __attribute__ ((unused)) _dump_ports_bmp(
  79. const struct ports_bmp* bmp, const char* func, int line)
  80. {
  81. pr_debug("%s:%d: ports_bmp: %016lxh\n", func, line, bmp->status);
  82. }
  83. static int ps3_vuart_match_id_to_port(enum ps3_match_id match_id,
  84. unsigned int *port_number)
  85. {
  86. switch(match_id) {
  87. case PS3_MATCH_ID_AV_SETTINGS:
  88. *port_number = 0;
  89. return 0;
  90. case PS3_MATCH_ID_SYSTEM_MANAGER:
  91. *port_number = 2;
  92. return 0;
  93. default:
  94. WARN_ON(1);
  95. *port_number = UINT_MAX;
  96. return -EINVAL;
  97. };
  98. }
  99. #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
  100. static void __attribute__ ((unused)) _dump_port_params(unsigned int port_number,
  101. const char* func, int line)
  102. {
  103. #if defined(DEBUG)
  104. static const char *strings[] = {
  105. "tx_trigger ",
  106. "rx_trigger ",
  107. "interrupt_mask ",
  108. "rx_buf_size ",
  109. "rx_bytes ",
  110. "tx_buf_size ",
  111. "tx_bytes ",
  112. "interrupt_status",
  113. };
  114. int result;
  115. unsigned int i;
  116. u64 value;
  117. for (i = 0; i < ARRAY_SIZE(strings); i++) {
  118. result = lv1_get_virtual_uart_param(port_number, i, &value);
  119. if (result) {
  120. pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
  121. port_number, strings[i], ps3_result(result));
  122. continue;
  123. }
  124. pr_debug("%s:%d: port_%u: %s = %lxh\n",
  125. func, line, port_number, strings[i], value);
  126. }
  127. #endif
  128. }
  129. struct vuart_triggers {
  130. unsigned long rx;
  131. unsigned long tx;
  132. };
  133. int ps3_vuart_get_triggers(struct ps3_vuart_port_device *dev,
  134. struct vuart_triggers *trig)
  135. {
  136. int result;
  137. unsigned long size;
  138. unsigned long val;
  139. result = lv1_get_virtual_uart_param(dev->port_number,
  140. PARAM_TX_TRIGGER, &trig->tx);
  141. if (result) {
  142. dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
  143. __func__, __LINE__, ps3_result(result));
  144. return result;
  145. }
  146. result = lv1_get_virtual_uart_param(dev->port_number,
  147. PARAM_RX_BUF_SIZE, &size);
  148. if (result) {
  149. dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
  150. __func__, __LINE__, ps3_result(result));
  151. return result;
  152. }
  153. result = lv1_get_virtual_uart_param(dev->port_number,
  154. PARAM_RX_TRIGGER, &val);
  155. if (result) {
  156. dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
  157. __func__, __LINE__, ps3_result(result));
  158. return result;
  159. }
  160. trig->rx = size - val;
  161. dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
  162. trig->tx, trig->rx);
  163. return result;
  164. }
  165. int ps3_vuart_set_triggers(struct ps3_vuart_port_device *dev, unsigned int tx,
  166. unsigned int rx)
  167. {
  168. int result;
  169. unsigned long size;
  170. result = lv1_set_virtual_uart_param(dev->port_number,
  171. PARAM_TX_TRIGGER, tx);
  172. if (result) {
  173. dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
  174. __func__, __LINE__, ps3_result(result));
  175. return result;
  176. }
  177. result = lv1_get_virtual_uart_param(dev->port_number,
  178. PARAM_RX_BUF_SIZE, &size);
  179. if (result) {
  180. dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
  181. __func__, __LINE__, ps3_result(result));
  182. return result;
  183. }
  184. result = lv1_set_virtual_uart_param(dev->port_number,
  185. PARAM_RX_TRIGGER, size - rx);
  186. if (result) {
  187. dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
  188. __func__, __LINE__, ps3_result(result));
  189. return result;
  190. }
  191. dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
  192. tx, rx);
  193. return result;
  194. }
  195. static int ps3_vuart_get_rx_bytes_waiting(struct ps3_vuart_port_device *dev,
  196. unsigned long *bytes_waiting)
  197. {
  198. int result = lv1_get_virtual_uart_param(dev->port_number,
  199. PARAM_RX_BYTES, bytes_waiting);
  200. if (result)
  201. dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
  202. __func__, __LINE__, ps3_result(result));
  203. dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__,
  204. *bytes_waiting);
  205. return result;
  206. }
  207. static int ps3_vuart_set_interrupt_mask(struct ps3_vuart_port_device *dev,
  208. unsigned long mask)
  209. {
  210. int result;
  211. dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
  212. dev->interrupt_mask = mask;
  213. result = lv1_set_virtual_uart_param(dev->port_number,
  214. PARAM_INTERRUPT_MASK, dev->interrupt_mask);
  215. if (result)
  216. dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
  217. __func__, __LINE__, ps3_result(result));
  218. return result;
  219. }
  220. static int ps3_vuart_get_interrupt_mask(struct ps3_vuart_port_device *dev,
  221. unsigned long *status)
  222. {
  223. int result = lv1_get_virtual_uart_param(dev->port_number,
  224. PARAM_INTERRUPT_STATUS, status);
  225. if (result)
  226. dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
  227. __func__, __LINE__, ps3_result(result));
  228. dev_dbg(&dev->core, "%s:%d: m %lxh, s %lxh, m&s %lxh\n",
  229. __func__, __LINE__, dev->interrupt_mask, *status,
  230. dev->interrupt_mask & *status);
  231. return result;
  232. }
  233. int ps3_vuart_enable_interrupt_tx(struct ps3_vuart_port_device *dev)
  234. {
  235. return (dev->interrupt_mask & INTERRUPT_MASK_TX) ? 0
  236. : ps3_vuart_set_interrupt_mask(dev, dev->interrupt_mask
  237. | INTERRUPT_MASK_TX);
  238. }
  239. int ps3_vuart_enable_interrupt_rx(struct ps3_vuart_port_device *dev)
  240. {
  241. return (dev->interrupt_mask & INTERRUPT_MASK_RX) ? 0
  242. : ps3_vuart_set_interrupt_mask(dev, dev->interrupt_mask
  243. | INTERRUPT_MASK_RX);
  244. }
  245. int ps3_vuart_enable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
  246. {
  247. return (dev->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
  248. : ps3_vuart_set_interrupt_mask(dev, dev->interrupt_mask
  249. | INTERRUPT_MASK_DISCONNECT);
  250. }
  251. int ps3_vuart_disable_interrupt_tx(struct ps3_vuart_port_device *dev)
  252. {
  253. return (dev->interrupt_mask & INTERRUPT_MASK_TX)
  254. ? ps3_vuart_set_interrupt_mask(dev, dev->interrupt_mask
  255. & ~INTERRUPT_MASK_TX) : 0;
  256. }
  257. int ps3_vuart_disable_interrupt_rx(struct ps3_vuart_port_device *dev)
  258. {
  259. return (dev->interrupt_mask & INTERRUPT_MASK_RX)
  260. ? ps3_vuart_set_interrupt_mask(dev, dev->interrupt_mask
  261. & ~INTERRUPT_MASK_RX) : 0;
  262. }
  263. int ps3_vuart_disable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
  264. {
  265. return (dev->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
  266. ? ps3_vuart_set_interrupt_mask(dev, dev->interrupt_mask
  267. & ~INTERRUPT_MASK_DISCONNECT) : 0;
  268. }
  269. /**
  270. * ps3_vuart_raw_write - Low level write helper.
  271. *
  272. * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
  273. */
  274. static int ps3_vuart_raw_write(struct ps3_vuart_port_device *dev,
  275. const void* buf, unsigned int bytes, unsigned long *bytes_written)
  276. {
  277. int result;
  278. dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
  279. result = lv1_write_virtual_uart(dev->port_number,
  280. ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
  281. if (result) {
  282. dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
  283. "%s\n", __func__, __LINE__, ps3_result(result));
  284. return result;
  285. }
  286. dev->stats.bytes_written += *bytes_written;
  287. dev_dbg(&dev->core, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__,
  288. __LINE__, *bytes_written, bytes, dev->stats.bytes_written);
  289. return result;
  290. }
  291. /**
  292. * ps3_vuart_raw_read - Low level read helper.
  293. *
  294. * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
  295. */
  296. static int ps3_vuart_raw_read(struct ps3_vuart_port_device *dev, void* buf,
  297. unsigned int bytes, unsigned long *bytes_read)
  298. {
  299. int result;
  300. dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
  301. result = lv1_read_virtual_uart(dev->port_number,
  302. ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
  303. if (result) {
  304. dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
  305. __func__, __LINE__, ps3_result(result));
  306. return result;
  307. }
  308. dev->stats.bytes_read += *bytes_read;
  309. dev_dbg(&dev->core, "%s:%d: read %lxh/%xh=>%lxh\n", __func__, __LINE__,
  310. *bytes_read, bytes, dev->stats.bytes_read);
  311. return result;
  312. }
  313. /**
  314. * struct list_buffer - An element for a port device fifo buffer list.
  315. */
  316. struct list_buffer {
  317. struct list_head link;
  318. const unsigned char *head;
  319. const unsigned char *tail;
  320. unsigned long dbg_number;
  321. unsigned char data[];
  322. };
  323. /**
  324. * ps3_vuart_write - the entry point for writing data to a port
  325. *
  326. * If the port is idle on entry as much of the incoming data is written to
  327. * the port as the port will accept. Otherwise a list buffer is created
  328. * and any remaning incoming data is copied to that buffer. The buffer is
  329. * then enqueued for transmision via the transmit interrupt.
  330. */
  331. int ps3_vuart_write(struct ps3_vuart_port_device *dev, const void* buf,
  332. unsigned int bytes)
  333. {
  334. static unsigned long dbg_number;
  335. int result;
  336. unsigned long flags;
  337. struct list_buffer *lb;
  338. dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
  339. bytes, bytes);
  340. spin_lock_irqsave(&dev->tx_list.lock, flags);
  341. if (list_empty(&dev->tx_list.head)) {
  342. unsigned long bytes_written;
  343. result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
  344. spin_unlock_irqrestore(&dev->tx_list.lock, flags);
  345. if (result) {
  346. dev_dbg(&dev->core,
  347. "%s:%d: ps3_vuart_raw_write failed\n",
  348. __func__, __LINE__);
  349. return result;
  350. }
  351. if (bytes_written == bytes) {
  352. dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
  353. __func__, __LINE__, bytes);
  354. return 0;
  355. }
  356. bytes -= bytes_written;
  357. buf += bytes_written;
  358. } else
  359. spin_unlock_irqrestore(&dev->tx_list.lock, flags);
  360. lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
  361. if (!lb) {
  362. return -ENOMEM;
  363. }
  364. memcpy(lb->data, buf, bytes);
  365. lb->head = lb->data;
  366. lb->tail = lb->data + bytes;
  367. lb->dbg_number = ++dbg_number;
  368. spin_lock_irqsave(&dev->tx_list.lock, flags);
  369. list_add_tail(&lb->link, &dev->tx_list.head);
  370. ps3_vuart_enable_interrupt_tx(dev);
  371. spin_unlock_irqrestore(&dev->tx_list.lock, flags);
  372. dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
  373. __func__, __LINE__, lb->dbg_number, bytes);
  374. return 0;
  375. }
  376. /**
  377. * ps3_vuart_read - the entry point for reading data from a port
  378. *
  379. * If enough bytes to satisfy the request are held in the buffer list those
  380. * bytes are dequeued and copied to the caller's buffer. Emptied list buffers
  381. * are retiered. If the request cannot be statified by bytes held in the list
  382. * buffers -EAGAIN is returned.
  383. */
  384. int ps3_vuart_read(struct ps3_vuart_port_device *dev, void* buf,
  385. unsigned int bytes)
  386. {
  387. unsigned long flags;
  388. struct list_buffer *lb, *n;
  389. unsigned long bytes_read;
  390. dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
  391. bytes, bytes);
  392. spin_lock_irqsave(&dev->rx_list.lock, flags);
  393. if (dev->rx_list.bytes_held < bytes) {
  394. spin_unlock_irqrestore(&dev->rx_list.lock, flags);
  395. dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
  396. __func__, __LINE__, bytes - dev->rx_list.bytes_held);
  397. return -EAGAIN;
  398. }
  399. list_for_each_entry_safe(lb, n, &dev->rx_list.head, link) {
  400. bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
  401. memcpy(buf, lb->head, bytes_read);
  402. buf += bytes_read;
  403. bytes -= bytes_read;
  404. dev->rx_list.bytes_held -= bytes_read;
  405. if (bytes_read < lb->tail - lb->head) {
  406. lb->head += bytes_read;
  407. spin_unlock_irqrestore(&dev->rx_list.lock, flags);
  408. dev_dbg(&dev->core,
  409. "%s:%d: dequeued buf_%lu, %lxh bytes\n",
  410. __func__, __LINE__, lb->dbg_number, bytes_read);
  411. return 0;
  412. }
  413. dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
  414. lb->dbg_number);
  415. list_del(&lb->link);
  416. kfree(lb);
  417. }
  418. spin_unlock_irqrestore(&dev->rx_list.lock, flags);
  419. dev_dbg(&dev->core, "%s:%d: dequeued buf_%lu, %xh bytes\n",
  420. __func__, __LINE__, lb->dbg_number, bytes);
  421. return 0;
  422. }
  423. /**
  424. * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
  425. *
  426. * Services the transmit interrupt for the port. Writes as much data from the
  427. * buffer list as the port will accept. Retires any emptied list buffers and
  428. * adjusts the final list buffer state for a partial write.
  429. */
  430. static int ps3_vuart_handle_interrupt_tx(struct ps3_vuart_port_device *dev)
  431. {
  432. int result = 0;
  433. unsigned long flags;
  434. struct list_buffer *lb, *n;
  435. unsigned long bytes_total = 0;
  436. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  437. spin_lock_irqsave(&dev->tx_list.lock, flags);
  438. list_for_each_entry_safe(lb, n, &dev->tx_list.head, link) {
  439. unsigned long bytes_written;
  440. result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
  441. &bytes_written);
  442. if (result) {
  443. dev_dbg(&dev->core,
  444. "%s:%d: ps3_vuart_raw_write failed\n",
  445. __func__, __LINE__);
  446. break;
  447. }
  448. bytes_total += bytes_written;
  449. if (bytes_written < lb->tail - lb->head) {
  450. lb->head += bytes_written;
  451. dev_dbg(&dev->core,
  452. "%s:%d cleared buf_%lu, %lxh bytes\n",
  453. __func__, __LINE__, lb->dbg_number,
  454. bytes_written);
  455. goto port_full;
  456. }
  457. dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
  458. lb->dbg_number);
  459. list_del(&lb->link);
  460. kfree(lb);
  461. }
  462. ps3_vuart_disable_interrupt_tx(dev);
  463. port_full:
  464. spin_unlock_irqrestore(&dev->tx_list.lock, flags);
  465. dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
  466. __func__, __LINE__, bytes_total);
  467. return result;
  468. }
  469. /**
  470. * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
  471. *
  472. * Services the receive interrupt for the port. Creates a list buffer and
  473. * copies all waiting port data to that buffer and enqueues the buffer in the
  474. * buffer list. Buffer list data is dequeued via ps3_vuart_read.
  475. */
  476. static int ps3_vuart_handle_interrupt_rx(struct ps3_vuart_port_device *dev)
  477. {
  478. static unsigned long dbg_number;
  479. int result = 0;
  480. unsigned long flags;
  481. struct list_buffer *lb;
  482. unsigned long bytes;
  483. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  484. result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
  485. if (result)
  486. return -EIO;
  487. BUG_ON(!bytes);
  488. /* add some extra space for recently arrived data */
  489. bytes += 128;
  490. lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
  491. if (!lb)
  492. return -ENOMEM;
  493. ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
  494. lb->head = lb->data;
  495. lb->tail = lb->data + bytes;
  496. lb->dbg_number = ++dbg_number;
  497. spin_lock_irqsave(&dev->rx_list.lock, flags);
  498. list_add_tail(&lb->link, &dev->rx_list.head);
  499. dev->rx_list.bytes_held += bytes;
  500. spin_unlock_irqrestore(&dev->rx_list.lock, flags);
  501. dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %lxh bytes\n",
  502. __func__, __LINE__, lb->dbg_number, bytes);
  503. return 0;
  504. }
  505. static int ps3_vuart_handle_interrupt_disconnect(
  506. struct ps3_vuart_port_device *dev)
  507. {
  508. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  509. BUG_ON("no support");
  510. return -1;
  511. }
  512. /**
  513. * ps3_vuart_handle_port_interrupt - second stage interrupt handler
  514. *
  515. * Services any pending interrupt types for the port. Passes control to the
  516. * third stage type specific interrupt handler. Returns control to the first
  517. * stage handler after one iteration.
  518. */
  519. static int ps3_vuart_handle_port_interrupt(struct ps3_vuart_port_device *dev)
  520. {
  521. int result;
  522. unsigned long status;
  523. result = ps3_vuart_get_interrupt_mask(dev, &status);
  524. if (result)
  525. return result;
  526. dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
  527. status);
  528. if (status & INTERRUPT_MASK_DISCONNECT) {
  529. dev->stats.disconnect_interrupts++;
  530. result = ps3_vuart_handle_interrupt_disconnect(dev);
  531. if (result)
  532. ps3_vuart_disable_interrupt_disconnect(dev);
  533. }
  534. if (status & INTERRUPT_MASK_TX) {
  535. dev->stats.tx_interrupts++;
  536. result = ps3_vuart_handle_interrupt_tx(dev);
  537. if (result)
  538. ps3_vuart_disable_interrupt_tx(dev);
  539. }
  540. if (status & INTERRUPT_MASK_RX) {
  541. dev->stats.rx_interrupts++;
  542. result = ps3_vuart_handle_interrupt_rx(dev);
  543. if (result)
  544. ps3_vuart_disable_interrupt_rx(dev);
  545. }
  546. return 0;
  547. }
  548. struct vuart_private {
  549. unsigned int in_use;
  550. unsigned int virq;
  551. struct ps3_vuart_port_device *devices[PORT_COUNT];
  552. const struct ports_bmp bmp;
  553. };
  554. /**
  555. * ps3_vuart_irq_handler - first stage interrupt handler
  556. *
  557. * Loops finding any interrupting port and its associated instance data.
  558. * Passes control to the second stage port specific interrupt handler. Loops
  559. * until all outstanding interrupts are serviced.
  560. */
  561. static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
  562. {
  563. struct vuart_private *private;
  564. BUG_ON(!_private);
  565. private = (struct vuart_private *)_private;
  566. while (1) {
  567. unsigned int port;
  568. dump_ports_bmp(&private->bmp);
  569. port = (BITS_PER_LONG - 1) - __ilog2(private->bmp.status);
  570. if (port == BITS_PER_LONG)
  571. break;
  572. BUG_ON(port >= PORT_COUNT);
  573. BUG_ON(!private->devices[port]);
  574. ps3_vuart_handle_port_interrupt(private->devices[port]);
  575. }
  576. return IRQ_HANDLED;
  577. }
  578. static int ps3_vuart_match(struct device *_dev, struct device_driver *_drv)
  579. {
  580. int result;
  581. struct ps3_vuart_port_driver *drv = to_ps3_vuart_port_driver(_drv);
  582. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  583. result = dev->match_id == drv->match_id;
  584. dev_info(&dev->core, "%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__,
  585. __LINE__, dev->match_id, dev->core.bus_id, drv->match_id,
  586. drv->core.name, (result ? "match" : "miss"));
  587. return result;
  588. }
  589. static struct vuart_private vuart_private;
  590. static int ps3_vuart_probe(struct device *_dev)
  591. {
  592. int result;
  593. unsigned long tmp;
  594. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  595. struct ps3_vuart_port_driver *drv =
  596. to_ps3_vuart_port_driver(_dev->driver);
  597. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  598. BUG_ON(!drv);
  599. result = ps3_vuart_match_id_to_port(dev->match_id, &dev->port_number);
  600. if (result) {
  601. dev_dbg(&dev->core, "%s:%d: unknown match_id (%d)\n",
  602. __func__, __LINE__, dev->match_id);
  603. result = -EINVAL;
  604. goto fail_match;
  605. }
  606. if (vuart_private.devices[dev->port_number]) {
  607. dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
  608. __LINE__, dev->port_number);
  609. result = -EBUSY;
  610. goto fail_match;
  611. }
  612. vuart_private.devices[dev->port_number] = dev;
  613. INIT_LIST_HEAD(&dev->tx_list.head);
  614. spin_lock_init(&dev->tx_list.lock);
  615. INIT_LIST_HEAD(&dev->rx_list.head);
  616. spin_lock_init(&dev->rx_list.lock);
  617. vuart_private.in_use++;
  618. if (vuart_private.in_use == 1) {
  619. result = ps3_alloc_vuart_irq(PS3_BINDING_CPU_ANY,
  620. (void*)&vuart_private.bmp.status, &vuart_private.virq);
  621. if (result) {
  622. dev_dbg(&dev->core,
  623. "%s:%d: ps3_alloc_vuart_irq failed (%d)\n",
  624. __func__, __LINE__, result);
  625. result = -EPERM;
  626. goto fail_alloc_irq;
  627. }
  628. result = request_irq(vuart_private.virq, ps3_vuart_irq_handler,
  629. IRQF_DISABLED, "vuart", &vuart_private);
  630. if (result) {
  631. dev_info(&dev->core, "%s:%d: request_irq failed (%d)\n",
  632. __func__, __LINE__, result);
  633. goto fail_request_irq;
  634. }
  635. }
  636. ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
  637. /* clear stale pending interrupts */
  638. ps3_vuart_get_interrupt_mask(dev, &tmp);
  639. ps3_vuart_set_triggers(dev, 1, 1);
  640. if (drv->probe)
  641. result = drv->probe(dev);
  642. else {
  643. result = 0;
  644. dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
  645. __LINE__);
  646. }
  647. if (result) {
  648. dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
  649. __func__, __LINE__);
  650. goto fail_probe;
  651. }
  652. return result;
  653. fail_probe:
  654. fail_request_irq:
  655. vuart_private.in_use--;
  656. if (!vuart_private.in_use) {
  657. ps3_free_vuart_irq(vuart_private.virq);
  658. vuart_private.virq = NO_IRQ;
  659. }
  660. fail_alloc_irq:
  661. fail_match:
  662. dev_dbg(&dev->core, "%s:%d failed\n", __func__, __LINE__);
  663. return result;
  664. }
  665. static int ps3_vuart_remove(struct device *_dev)
  666. {
  667. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  668. struct ps3_vuart_port_driver *drv =
  669. to_ps3_vuart_port_driver(_dev->driver);
  670. dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
  671. dev->core.bus_id);
  672. BUG_ON(vuart_private.in_use < 1);
  673. if (drv->remove)
  674. drv->remove(dev);
  675. else
  676. dev_dbg(&dev->core, "%s:%d: %s no remove method\n", __func__,
  677. __LINE__, dev->core.bus_id);
  678. vuart_private.in_use--;
  679. if (!vuart_private.in_use) {
  680. free_irq(vuart_private.virq, &vuart_private);
  681. ps3_free_vuart_irq(vuart_private.virq);
  682. vuart_private.virq = NO_IRQ;
  683. }
  684. return 0;
  685. }
  686. /**
  687. * ps3_vuart - The vuart instance.
  688. *
  689. * The vuart is managed as a bus that port devices connect to.
  690. */
  691. struct bus_type ps3_vuart = {
  692. .name = "ps3_vuart",
  693. .match = ps3_vuart_match,
  694. .probe = ps3_vuart_probe,
  695. .remove = ps3_vuart_remove,
  696. };
  697. int __init ps3_vuart_init(void)
  698. {
  699. int result;
  700. pr_debug("%s:%d:\n", __func__, __LINE__);
  701. result = bus_register(&ps3_vuart);
  702. BUG_ON(result);
  703. return result;
  704. }
  705. void __exit ps3_vuart_exit(void)
  706. {
  707. pr_debug("%s:%d:\n", __func__, __LINE__);
  708. bus_unregister(&ps3_vuart);
  709. }
  710. core_initcall(ps3_vuart_init);
  711. module_exit(ps3_vuart_exit);
  712. /**
  713. * ps3_vuart_port_release_device - Remove a vuart port device.
  714. */
  715. static void ps3_vuart_port_release_device(struct device *_dev)
  716. {
  717. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  718. #if defined(DEBUG)
  719. memset(dev, 0xad, sizeof(struct ps3_vuart_port_device));
  720. #endif
  721. kfree(dev);
  722. }
  723. /**
  724. * ps3_vuart_port_device_register - Add a vuart port device.
  725. */
  726. int ps3_vuart_port_device_register(struct ps3_vuart_port_device *dev)
  727. {
  728. int result;
  729. static unsigned int dev_count = 1;
  730. dev->core.parent = NULL;
  731. dev->core.bus = &ps3_vuart;
  732. dev->core.release = ps3_vuart_port_release_device;
  733. snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "vuart_%02x",
  734. dev_count++);
  735. dev_dbg(&dev->core, "%s:%d register\n", __func__, __LINE__);
  736. result = device_register(&dev->core);
  737. return result;
  738. }
  739. EXPORT_SYMBOL_GPL(ps3_vuart_port_device_register);
  740. /**
  741. * ps3_vuart_port_driver_register - Add a vuart port device driver.
  742. */
  743. int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
  744. {
  745. int result;
  746. pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
  747. drv->core.bus = &ps3_vuart;
  748. result = driver_register(&drv->core);
  749. return result;
  750. }
  751. EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
  752. /**
  753. * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
  754. */
  755. void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
  756. {
  757. driver_unregister(&drv->core);
  758. }
  759. EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);