ps3av.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /*
  2. * PS3 AV backend support.
  3. *
  4. * Copyright (C) 2007 Sony Computer Entertainment Inc.
  5. * Copyright 2007 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/delay.h>
  23. #include <linux/notifier.h>
  24. #include <linux/ioctl.h>
  25. #include <asm/firmware.h>
  26. #include <asm/ps3av.h>
  27. #include <asm/ps3.h>
  28. #include "vuart.h"
  29. #define BUFSIZE 4096 /* vuart buf size */
  30. #define PS3AV_BUF_SIZE 512 /* max packet size */
  31. static int timeout = 5000; /* in msec ( 5 sec ) */
  32. module_param(timeout, int, 0644);
  33. static struct ps3av {
  34. struct mutex mutex;
  35. struct work_struct work;
  36. struct completion done;
  37. struct workqueue_struct *wq;
  38. int open_count;
  39. struct ps3_system_bus_device *dev;
  40. int region;
  41. struct ps3av_pkt_av_get_hw_conf av_hw_conf;
  42. u32 av_port[PS3AV_AV_PORT_MAX + PS3AV_OPT_PORT_MAX];
  43. u32 opt_port[PS3AV_OPT_PORT_MAX];
  44. u32 head[PS3AV_HEAD_MAX];
  45. u32 audio_port;
  46. int ps3av_mode;
  47. int ps3av_mode_old;
  48. union {
  49. struct ps3av_reply_hdr reply_hdr;
  50. u8 raw[PS3AV_BUF_SIZE];
  51. } recv_buf;
  52. void (*flip_ctl)(int on, void *data);
  53. void *flip_data;
  54. } *ps3av;
  55. /* color space */
  56. #define YUV444 PS3AV_CMD_VIDEO_CS_YUV444_8
  57. #define RGB8 PS3AV_CMD_VIDEO_CS_RGB_8
  58. /* format */
  59. #define XRGB PS3AV_CMD_VIDEO_FMT_X8R8G8B8
  60. /* aspect */
  61. #define A_N PS3AV_CMD_AV_ASPECT_4_3
  62. #define A_W PS3AV_CMD_AV_ASPECT_16_9
  63. static const struct avset_video_mode {
  64. u32 cs;
  65. u32 fmt;
  66. u32 vid;
  67. u32 aspect;
  68. u32 x;
  69. u32 y;
  70. u32 interlace;
  71. u32 freq;
  72. } video_mode_table[] = {
  73. { 0, }, /* auto */
  74. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_480I, A_N, 720, 480, 1, 60},
  75. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_480P, A_N, 720, 480, 0, 60},
  76. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_720P_60HZ, A_N, 1280, 720, 0, 60},
  77. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080I_60HZ, A_W, 1920, 1080, 1, 60},
  78. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080P_60HZ, A_W, 1920, 1080, 0, 60},
  79. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_576I, A_N, 720, 576, 1, 50},
  80. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_576P, A_N, 720, 576, 0, 50},
  81. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_720P_50HZ, A_N, 1280, 720, 0, 50},
  82. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080I_50HZ, A_W, 1920, 1080, 1, 50},
  83. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080P_50HZ, A_W, 1920, 1080, 0, 50},
  84. { RGB8, XRGB, PS3AV_CMD_VIDEO_VID_WXGA, A_W, 1280, 768, 0, 60},
  85. { RGB8, XRGB, PS3AV_CMD_VIDEO_VID_SXGA, A_N, 1280, 1024, 0, 60},
  86. { RGB8, XRGB, PS3AV_CMD_VIDEO_VID_WUXGA, A_W, 1920, 1200, 0, 60},
  87. };
  88. /* supported CIDs */
  89. static u32 cmd_table[] = {
  90. /* init */
  91. PS3AV_CID_AV_INIT,
  92. PS3AV_CID_AV_FIN,
  93. PS3AV_CID_VIDEO_INIT,
  94. PS3AV_CID_AUDIO_INIT,
  95. /* set */
  96. PS3AV_CID_AV_ENABLE_EVENT,
  97. PS3AV_CID_AV_DISABLE_EVENT,
  98. PS3AV_CID_AV_VIDEO_CS,
  99. PS3AV_CID_AV_VIDEO_MUTE,
  100. PS3AV_CID_AV_VIDEO_DISABLE_SIG,
  101. PS3AV_CID_AV_AUDIO_PARAM,
  102. PS3AV_CID_AV_AUDIO_MUTE,
  103. PS3AV_CID_AV_HDMI_MODE,
  104. PS3AV_CID_AV_TV_MUTE,
  105. PS3AV_CID_VIDEO_MODE,
  106. PS3AV_CID_VIDEO_FORMAT,
  107. PS3AV_CID_VIDEO_PITCH,
  108. PS3AV_CID_AUDIO_MODE,
  109. PS3AV_CID_AUDIO_MUTE,
  110. PS3AV_CID_AUDIO_ACTIVE,
  111. PS3AV_CID_AUDIO_INACTIVE,
  112. PS3AV_CID_AVB_PARAM,
  113. /* get */
  114. PS3AV_CID_AV_GET_HW_CONF,
  115. PS3AV_CID_AV_GET_MONITOR_INFO,
  116. /* event */
  117. PS3AV_CID_EVENT_UNPLUGGED,
  118. PS3AV_CID_EVENT_PLUGGED,
  119. PS3AV_CID_EVENT_HDCP_DONE,
  120. PS3AV_CID_EVENT_HDCP_FAIL,
  121. PS3AV_CID_EVENT_HDCP_AUTH,
  122. PS3AV_CID_EVENT_HDCP_ERROR,
  123. 0
  124. };
  125. #define PS3AV_EVENT_CMD_MASK 0x10000000
  126. #define PS3AV_EVENT_ID_MASK 0x0000ffff
  127. #define PS3AV_CID_MASK 0xffffffff
  128. #define PS3AV_REPLY_BIT 0x80000000
  129. #define ps3av_event_get_port_id(cid) ((cid >> 16) & 0xff)
  130. static u32 *ps3av_search_cmd_table(u32 cid, u32 mask)
  131. {
  132. u32 *table;
  133. int i;
  134. table = cmd_table;
  135. for (i = 0;; table++, i++) {
  136. if ((*table & mask) == (cid & mask))
  137. break;
  138. if (*table == 0)
  139. return NULL;
  140. }
  141. return table;
  142. }
  143. static int ps3av_parse_event_packet(const struct ps3av_reply_hdr *hdr)
  144. {
  145. u32 *table;
  146. if (hdr->cid & PS3AV_EVENT_CMD_MASK) {
  147. table = ps3av_search_cmd_table(hdr->cid, PS3AV_EVENT_CMD_MASK);
  148. if (table)
  149. dev_dbg(&ps3av->dev->core,
  150. "recv event packet cid:%08x port:0x%x size:%d\n",
  151. hdr->cid, ps3av_event_get_port_id(hdr->cid),
  152. hdr->size);
  153. else
  154. printk(KERN_ERR
  155. "%s: failed event packet, cid:%08x size:%d\n",
  156. __func__, hdr->cid, hdr->size);
  157. return 1; /* receive event packet */
  158. }
  159. return 0;
  160. }
  161. #define POLLING_INTERVAL 25 /* in msec */
  162. static int ps3av_vuart_write(struct ps3_system_bus_device *dev,
  163. const void *buf, unsigned long size)
  164. {
  165. int error;
  166. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  167. error = ps3_vuart_write(dev, buf, size);
  168. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  169. return error ? error : size;
  170. }
  171. static int ps3av_vuart_read(struct ps3_system_bus_device *dev, void *buf,
  172. unsigned long size, int timeout)
  173. {
  174. int error;
  175. int loopcnt = 0;
  176. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  177. timeout = (timeout + POLLING_INTERVAL - 1) / POLLING_INTERVAL;
  178. while (loopcnt++ <= timeout) {
  179. error = ps3_vuart_read(dev, buf, size);
  180. if (!error)
  181. return size;
  182. if (error != -EAGAIN) {
  183. printk(KERN_ERR "%s: ps3_vuart_read failed %d\n",
  184. __func__, error);
  185. return error;
  186. }
  187. msleep(POLLING_INTERVAL);
  188. }
  189. return -EWOULDBLOCK;
  190. }
  191. static int ps3av_send_cmd_pkt(const struct ps3av_send_hdr *send_buf,
  192. struct ps3av_reply_hdr *recv_buf, int write_len,
  193. int read_len)
  194. {
  195. int res;
  196. u32 cmd;
  197. int event;
  198. if (!ps3av)
  199. return -ENODEV;
  200. /* send pkt */
  201. res = ps3av_vuart_write(ps3av->dev, send_buf, write_len);
  202. if (res < 0) {
  203. dev_dbg(&ps3av->dev->core,
  204. "%s: ps3av_vuart_write() failed (result=%d)\n",
  205. __func__, res);
  206. return res;
  207. }
  208. /* recv pkt */
  209. cmd = send_buf->cid;
  210. do {
  211. /* read header */
  212. res = ps3av_vuart_read(ps3av->dev, recv_buf, PS3AV_HDR_SIZE,
  213. timeout);
  214. if (res != PS3AV_HDR_SIZE) {
  215. dev_dbg(&ps3av->dev->core,
  216. "%s: ps3av_vuart_read() failed (result=%d)\n",
  217. __func__, res);
  218. return res;
  219. }
  220. /* read body */
  221. res = ps3av_vuart_read(ps3av->dev, &recv_buf->cid,
  222. recv_buf->size, timeout);
  223. if (res < 0) {
  224. dev_dbg(&ps3av->dev->core,
  225. "%s: ps3av_vuart_read() failed (result=%d)\n",
  226. __func__, res);
  227. return res;
  228. }
  229. res += PS3AV_HDR_SIZE; /* total len */
  230. event = ps3av_parse_event_packet(recv_buf);
  231. /* ret > 0 event packet */
  232. } while (event);
  233. if ((cmd | PS3AV_REPLY_BIT) != recv_buf->cid) {
  234. dev_dbg(&ps3av->dev->core, "%s: reply err (result=%x)\n",
  235. __func__, recv_buf->cid);
  236. return -EINVAL;
  237. }
  238. return 0;
  239. }
  240. static int ps3av_process_reply_packet(struct ps3av_send_hdr *cmd_buf,
  241. const struct ps3av_reply_hdr *recv_buf,
  242. int user_buf_size)
  243. {
  244. int return_len;
  245. if (recv_buf->version != PS3AV_VERSION) {
  246. dev_dbg(&ps3av->dev->core, "reply_packet invalid version:%x\n",
  247. recv_buf->version);
  248. return -EFAULT;
  249. }
  250. return_len = recv_buf->size + PS3AV_HDR_SIZE;
  251. if (return_len > user_buf_size)
  252. return_len = user_buf_size;
  253. memcpy(cmd_buf, recv_buf, return_len);
  254. return 0; /* success */
  255. }
  256. void ps3av_set_hdr(u32 cid, u16 size, struct ps3av_send_hdr *hdr)
  257. {
  258. hdr->version = PS3AV_VERSION;
  259. hdr->size = size - PS3AV_HDR_SIZE;
  260. hdr->cid = cid;
  261. }
  262. int ps3av_do_pkt(u32 cid, u16 send_len, size_t usr_buf_size,
  263. struct ps3av_send_hdr *buf)
  264. {
  265. int res = 0;
  266. u32 *table;
  267. BUG_ON(!ps3av);
  268. mutex_lock(&ps3av->mutex);
  269. table = ps3av_search_cmd_table(cid, PS3AV_CID_MASK);
  270. BUG_ON(!table);
  271. BUG_ON(send_len < PS3AV_HDR_SIZE);
  272. BUG_ON(usr_buf_size < send_len);
  273. BUG_ON(usr_buf_size > PS3AV_BUF_SIZE);
  274. /* create header */
  275. ps3av_set_hdr(cid, send_len, buf);
  276. /* send packet via vuart */
  277. res = ps3av_send_cmd_pkt(buf, &ps3av->recv_buf.reply_hdr, send_len,
  278. usr_buf_size);
  279. if (res < 0) {
  280. printk(KERN_ERR
  281. "%s: ps3av_send_cmd_pkt() failed (result=%d)\n",
  282. __func__, res);
  283. goto err;
  284. }
  285. /* process reply packet */
  286. res = ps3av_process_reply_packet(buf, &ps3av->recv_buf.reply_hdr,
  287. usr_buf_size);
  288. if (res < 0) {
  289. printk(KERN_ERR "%s: put_return_status() failed (result=%d)\n",
  290. __func__, res);
  291. goto err;
  292. }
  293. mutex_unlock(&ps3av->mutex);
  294. return 0;
  295. err:
  296. mutex_unlock(&ps3av->mutex);
  297. printk(KERN_ERR "%s: failed cid:%x res:%d\n", __func__, cid, res);
  298. return res;
  299. }
  300. static int ps3av_set_av_video_mute(u32 mute)
  301. {
  302. int i, num_of_av_port, res;
  303. num_of_av_port = ps3av->av_hw_conf.num_of_hdmi +
  304. ps3av->av_hw_conf.num_of_avmulti;
  305. /* video mute on */
  306. for (i = 0; i < num_of_av_port; i++) {
  307. res = ps3av_cmd_av_video_mute(1, &ps3av->av_port[i], mute);
  308. if (res < 0)
  309. return -1;
  310. }
  311. return 0;
  312. }
  313. static int ps3av_set_video_disable_sig(void)
  314. {
  315. int i, num_of_hdmi_port, num_of_av_port, res;
  316. num_of_hdmi_port = ps3av->av_hw_conf.num_of_hdmi;
  317. num_of_av_port = ps3av->av_hw_conf.num_of_hdmi +
  318. ps3av->av_hw_conf.num_of_avmulti;
  319. /* tv mute */
  320. for (i = 0; i < num_of_hdmi_port; i++) {
  321. res = ps3av_cmd_av_tv_mute(ps3av->av_port[i],
  322. PS3AV_CMD_MUTE_ON);
  323. if (res < 0)
  324. return -1;
  325. }
  326. msleep(100);
  327. /* video mute on */
  328. for (i = 0; i < num_of_av_port; i++) {
  329. res = ps3av_cmd_av_video_disable_sig(ps3av->av_port[i]);
  330. if (res < 0)
  331. return -1;
  332. if (i < num_of_hdmi_port) {
  333. res = ps3av_cmd_av_tv_mute(ps3av->av_port[i],
  334. PS3AV_CMD_MUTE_OFF);
  335. if (res < 0)
  336. return -1;
  337. }
  338. }
  339. msleep(300);
  340. return 0;
  341. }
  342. static int ps3av_set_audio_mute(u32 mute)
  343. {
  344. int i, num_of_av_port, num_of_opt_port, res;
  345. num_of_av_port = ps3av->av_hw_conf.num_of_hdmi +
  346. ps3av->av_hw_conf.num_of_avmulti;
  347. num_of_opt_port = ps3av->av_hw_conf.num_of_spdif;
  348. for (i = 0; i < num_of_av_port; i++) {
  349. res = ps3av_cmd_av_audio_mute(1, &ps3av->av_port[i], mute);
  350. if (res < 0)
  351. return -1;
  352. }
  353. for (i = 0; i < num_of_opt_port; i++) {
  354. res = ps3av_cmd_audio_mute(1, &ps3av->opt_port[i], mute);
  355. if (res < 0)
  356. return -1;
  357. }
  358. return 0;
  359. }
  360. int ps3av_set_audio_mode(u32 ch, u32 fs, u32 word_bits, u32 format, u32 source)
  361. {
  362. struct ps3av_pkt_avb_param avb_param;
  363. int i, num_of_audio, vid, res;
  364. struct ps3av_pkt_audio_mode audio_mode;
  365. u32 len = 0;
  366. num_of_audio = ps3av->av_hw_conf.num_of_hdmi +
  367. ps3av->av_hw_conf.num_of_avmulti +
  368. ps3av->av_hw_conf.num_of_spdif;
  369. avb_param.num_of_video_pkt = 0;
  370. avb_param.num_of_audio_pkt = PS3AV_AVB_NUM_AUDIO; /* always 0 */
  371. avb_param.num_of_av_video_pkt = 0;
  372. avb_param.num_of_av_audio_pkt = ps3av->av_hw_conf.num_of_hdmi;
  373. vid = video_mode_table[ps3av->ps3av_mode].vid;
  374. /* audio mute */
  375. ps3av_set_audio_mute(PS3AV_CMD_MUTE_ON);
  376. /* audio inactive */
  377. res = ps3av_cmd_audio_active(0, ps3av->audio_port);
  378. if (res < 0)
  379. dev_dbg(&ps3av->dev->core,
  380. "ps3av_cmd_audio_active OFF failed\n");
  381. /* audio_pkt */
  382. for (i = 0; i < num_of_audio; i++) {
  383. ps3av_cmd_set_audio_mode(&audio_mode, ps3av->av_port[i], ch,
  384. fs, word_bits, format, source);
  385. if (i < ps3av->av_hw_conf.num_of_hdmi) {
  386. /* hdmi only */
  387. len += ps3av_cmd_set_av_audio_param(&avb_param.buf[len],
  388. ps3av->av_port[i],
  389. &audio_mode, vid);
  390. }
  391. /* audio_mode pkt should be sent separately */
  392. res = ps3av_cmd_audio_mode(&audio_mode);
  393. if (res < 0)
  394. dev_dbg(&ps3av->dev->core,
  395. "ps3av_cmd_audio_mode failed, port:%x\n", i);
  396. }
  397. /* send command using avb pkt */
  398. len += offsetof(struct ps3av_pkt_avb_param, buf);
  399. res = ps3av_cmd_avb_param(&avb_param, len);
  400. if (res < 0)
  401. dev_dbg(&ps3av->dev->core, "ps3av_cmd_avb_param failed\n");
  402. /* audio mute */
  403. ps3av_set_audio_mute(PS3AV_CMD_MUTE_OFF);
  404. /* audio active */
  405. res = ps3av_cmd_audio_active(1, ps3av->audio_port);
  406. if (res < 0)
  407. dev_dbg(&ps3av->dev->core,
  408. "ps3av_cmd_audio_active ON failed\n");
  409. return 0;
  410. }
  411. EXPORT_SYMBOL_GPL(ps3av_set_audio_mode);
  412. static int ps3av_set_videomode(void)
  413. {
  414. /* av video mute */
  415. ps3av_set_av_video_mute(PS3AV_CMD_MUTE_ON);
  416. /* wake up ps3avd to do the actual video mode setting */
  417. queue_work(ps3av->wq, &ps3av->work);
  418. return 0;
  419. }
  420. static void ps3av_set_videomode_packet(u32 id)
  421. {
  422. struct ps3av_pkt_avb_param avb_param;
  423. unsigned int i;
  424. u32 len = 0, av_video_cs;
  425. const struct avset_video_mode *video_mode;
  426. int res;
  427. video_mode = &video_mode_table[id & PS3AV_MODE_MASK];
  428. avb_param.num_of_video_pkt = PS3AV_AVB_NUM_VIDEO; /* num of head */
  429. avb_param.num_of_audio_pkt = 0;
  430. avb_param.num_of_av_video_pkt = ps3av->av_hw_conf.num_of_hdmi +
  431. ps3av->av_hw_conf.num_of_avmulti;
  432. avb_param.num_of_av_audio_pkt = 0;
  433. /* video_pkt */
  434. for (i = 0; i < avb_param.num_of_video_pkt; i++)
  435. len += ps3av_cmd_set_video_mode(&avb_param.buf[len],
  436. ps3av->head[i], video_mode->vid,
  437. video_mode->fmt, id);
  438. /* av_video_pkt */
  439. for (i = 0; i < avb_param.num_of_av_video_pkt; i++) {
  440. if (id & PS3AV_MODE_DVI || id & PS3AV_MODE_RGB)
  441. av_video_cs = RGB8;
  442. else
  443. av_video_cs = video_mode->cs;
  444. #ifndef PS3AV_HDMI_YUV
  445. if (ps3av->av_port[i] == PS3AV_CMD_AVPORT_HDMI_0 ||
  446. ps3av->av_port[i] == PS3AV_CMD_AVPORT_HDMI_1)
  447. av_video_cs = RGB8; /* use RGB for HDMI */
  448. #endif
  449. len += ps3av_cmd_set_av_video_cs(&avb_param.buf[len],
  450. ps3av->av_port[i],
  451. video_mode->vid, av_video_cs,
  452. video_mode->aspect, id);
  453. }
  454. /* send command using avb pkt */
  455. len += offsetof(struct ps3av_pkt_avb_param, buf);
  456. res = ps3av_cmd_avb_param(&avb_param, len);
  457. if (res == PS3AV_STATUS_NO_SYNC_HEAD)
  458. printk(KERN_WARNING
  459. "%s: Command failed. Please try your request again. \n",
  460. __func__);
  461. else if (res)
  462. dev_dbg(&ps3av->dev->core, "ps3av_cmd_avb_param failed\n");
  463. }
  464. static void ps3av_set_videomode_cont(u32 id, u32 old_id)
  465. {
  466. static int vesa = 0;
  467. int res;
  468. /* video signal off */
  469. ps3av_set_video_disable_sig();
  470. /*
  471. * AV backend needs non-VESA mode setting at least one time
  472. * when VESA mode is used.
  473. */
  474. if (vesa == 0 && (id & PS3AV_MODE_MASK) >= 11) {
  475. /* vesa mode */
  476. ps3av_set_videomode_packet(2); /* 480P */
  477. }
  478. vesa = 1;
  479. /* Retail PS3 product doesn't support this */
  480. if (id & PS3AV_MODE_HDCP_OFF) {
  481. res = ps3av_cmd_av_hdmi_mode(PS3AV_CMD_AV_HDMI_HDCP_OFF);
  482. if (res == PS3AV_STATUS_UNSUPPORTED_HDMI_MODE)
  483. dev_dbg(&ps3av->dev->core, "Not supported\n");
  484. else if (res)
  485. dev_dbg(&ps3av->dev->core,
  486. "ps3av_cmd_av_hdmi_mode failed\n");
  487. } else if (old_id & PS3AV_MODE_HDCP_OFF) {
  488. res = ps3av_cmd_av_hdmi_mode(PS3AV_CMD_AV_HDMI_MODE_NORMAL);
  489. if (res < 0 && res != PS3AV_STATUS_UNSUPPORTED_HDMI_MODE)
  490. dev_dbg(&ps3av->dev->core,
  491. "ps3av_cmd_av_hdmi_mode failed\n");
  492. }
  493. ps3av_set_videomode_packet(id);
  494. msleep(1500);
  495. /* av video mute */
  496. ps3av_set_av_video_mute(PS3AV_CMD_MUTE_OFF);
  497. }
  498. static void ps3avd(struct work_struct *work)
  499. {
  500. ps3av_set_videomode_cont(ps3av->ps3av_mode, ps3av->ps3av_mode_old);
  501. complete(&ps3av->done);
  502. }
  503. static int ps3av_vid2table_id(int vid)
  504. {
  505. int i;
  506. for (i = 1; i < ARRAY_SIZE(video_mode_table); i++)
  507. if (video_mode_table[i].vid == vid)
  508. return i;
  509. return -1;
  510. }
  511. static int ps3av_resbit2vid(u32 res_50, u32 res_60)
  512. {
  513. int vid = -1;
  514. if (res_50 > res_60) { /* if res_50 == res_60, res_60 will be used */
  515. if (res_50 & PS3AV_RESBIT_1920x1080P)
  516. vid = PS3AV_CMD_VIDEO_VID_1080P_50HZ;
  517. else if (res_50 & PS3AV_RESBIT_1920x1080I)
  518. vid = PS3AV_CMD_VIDEO_VID_1080I_50HZ;
  519. else if (res_50 & PS3AV_RESBIT_1280x720P)
  520. vid = PS3AV_CMD_VIDEO_VID_720P_50HZ;
  521. else if (res_50 & PS3AV_RESBIT_720x576P)
  522. vid = PS3AV_CMD_VIDEO_VID_576P;
  523. else
  524. vid = -1;
  525. } else {
  526. if (res_60 & PS3AV_RESBIT_1920x1080P)
  527. vid = PS3AV_CMD_VIDEO_VID_1080P_60HZ;
  528. else if (res_60 & PS3AV_RESBIT_1920x1080I)
  529. vid = PS3AV_CMD_VIDEO_VID_1080I_60HZ;
  530. else if (res_60 & PS3AV_RESBIT_1280x720P)
  531. vid = PS3AV_CMD_VIDEO_VID_720P_60HZ;
  532. else if (res_60 & PS3AV_RESBIT_720x480P)
  533. vid = PS3AV_CMD_VIDEO_VID_480P;
  534. else
  535. vid = -1;
  536. }
  537. return vid;
  538. }
  539. static int ps3av_hdmi_get_vid(struct ps3av_info_monitor *info)
  540. {
  541. u32 res_50, res_60;
  542. int vid = -1;
  543. if (info->monitor_type != PS3AV_MONITOR_TYPE_HDMI)
  544. return -1;
  545. /* check native resolution */
  546. res_50 = info->res_50.native & PS3AV_RES_MASK_50;
  547. res_60 = info->res_60.native & PS3AV_RES_MASK_60;
  548. if (res_50 || res_60) {
  549. vid = ps3av_resbit2vid(res_50, res_60);
  550. return vid;
  551. }
  552. /* check resolution */
  553. res_50 = info->res_50.res_bits & PS3AV_RES_MASK_50;
  554. res_60 = info->res_60.res_bits & PS3AV_RES_MASK_60;
  555. if (res_50 || res_60) {
  556. vid = ps3av_resbit2vid(res_50, res_60);
  557. return vid;
  558. }
  559. if (ps3av->region & PS3AV_REGION_60)
  560. vid = PS3AV_DEFAULT_HDMI_VID_REG_60;
  561. else
  562. vid = PS3AV_DEFAULT_HDMI_VID_REG_50;
  563. return vid;
  564. }
  565. static void ps3av_monitor_info_dump(const struct ps3av_pkt_av_get_monitor_info *monitor_info)
  566. {
  567. const struct ps3av_info_monitor *info = &monitor_info->info;
  568. const struct ps3av_info_audio *audio = info->audio;
  569. char id[sizeof(info->monitor_id)*3+1];
  570. int i;
  571. pr_debug("Monitor Info: size %u\n", monitor_info->send_hdr.size);
  572. pr_debug("avport: %02x\n", info->avport);
  573. for (i = 0; i < sizeof(info->monitor_id); i++)
  574. sprintf(&id[i*3], " %02x", info->monitor_id[i]);
  575. pr_debug("monitor_id: %s\n", id);
  576. pr_debug("monitor_type: %02x\n", info->monitor_type);
  577. pr_debug("monitor_name: %.*s\n", (int)sizeof(info->monitor_name),
  578. info->monitor_name);
  579. /* resolution */
  580. pr_debug("resolution_60: bits: %08x native: %08x\n",
  581. info->res_60.res_bits, info->res_60.native);
  582. pr_debug("resolution_50: bits: %08x native: %08x\n",
  583. info->res_50.res_bits, info->res_50.native);
  584. pr_debug("resolution_other: bits: %08x native: %08x\n",
  585. info->res_other.res_bits, info->res_other.native);
  586. pr_debug("resolution_vesa: bits: %08x native: %08x\n",
  587. info->res_vesa.res_bits, info->res_vesa.native);
  588. /* color space */
  589. pr_debug("color space rgb: %02x\n", info->cs.rgb);
  590. pr_debug("color space yuv444: %02x\n", info->cs.yuv444);
  591. pr_debug("color space yuv422: %02x\n", info->cs.yuv422);
  592. /* color info */
  593. pr_debug("color info red: X %04x Y %04x\n", info->color.red_x,
  594. info->color.red_y);
  595. pr_debug("color info green: X %04x Y %04x\n", info->color.green_x,
  596. info->color.green_y);
  597. pr_debug("color info blue: X %04x Y %04x\n", info->color.blue_x,
  598. info->color.blue_y);
  599. pr_debug("color info white: X %04x Y %04x\n", info->color.white_x,
  600. info->color.white_y);
  601. pr_debug("color info gamma: %08x\n", info->color.gamma);
  602. /* other info */
  603. pr_debug("supported_AI: %02x\n", info->supported_ai);
  604. pr_debug("speaker_info: %02x\n", info->speaker_info);
  605. pr_debug("num of audio: %02x\n", info->num_of_audio_block);
  606. /* audio block */
  607. for (i = 0; i < info->num_of_audio_block; i++) {
  608. pr_debug("audio[%d] type: %02x max_ch: %02x fs: %02x sbit: "
  609. "%02x\n",
  610. i, audio->type, audio->max_num_of_ch, audio->fs,
  611. audio->sbit);
  612. audio++;
  613. }
  614. }
  615. static int ps3av_auto_videomode(struct ps3av_pkt_av_get_hw_conf *av_hw_conf,
  616. int boot)
  617. {
  618. int i, res, vid = -1, dvi = 0, rgb = 0;
  619. struct ps3av_pkt_av_get_monitor_info monitor_info;
  620. struct ps3av_info_monitor *info;
  621. /* get vid for hdmi */
  622. for (i = 0; i < av_hw_conf->num_of_hdmi; i++) {
  623. res = ps3av_cmd_video_get_monitor_info(&monitor_info,
  624. PS3AV_CMD_AVPORT_HDMI_0 +
  625. i);
  626. if (res < 0)
  627. return -1;
  628. ps3av_monitor_info_dump(&monitor_info);
  629. info = &monitor_info.info;
  630. /* check DVI */
  631. if (info->monitor_type == PS3AV_MONITOR_TYPE_DVI) {
  632. dvi = PS3AV_MODE_DVI;
  633. break;
  634. }
  635. /* check HDMI */
  636. vid = ps3av_hdmi_get_vid(info);
  637. if (vid != -1) {
  638. /* got valid vid */
  639. break;
  640. }
  641. }
  642. if (dvi) {
  643. /* DVI mode */
  644. vid = PS3AV_DEFAULT_DVI_VID;
  645. } else if (vid == -1) {
  646. /* no HDMI interface or HDMI is off */
  647. if (ps3av->region & PS3AV_REGION_60)
  648. vid = PS3AV_DEFAULT_AVMULTI_VID_REG_60;
  649. else
  650. vid = PS3AV_DEFAULT_AVMULTI_VID_REG_50;
  651. if (ps3av->region & PS3AV_REGION_RGB)
  652. rgb = PS3AV_MODE_RGB;
  653. } else if (boot) {
  654. /* HDMI: using DEFAULT HDMI_VID while booting up */
  655. info = &monitor_info.info;
  656. if (ps3av->region & PS3AV_REGION_60) {
  657. if (info->res_60.res_bits & PS3AV_RESBIT_720x480P)
  658. vid = PS3AV_DEFAULT_HDMI_VID_REG_60;
  659. else if (info->res_50.res_bits & PS3AV_RESBIT_720x576P)
  660. vid = PS3AV_DEFAULT_HDMI_VID_REG_50;
  661. else {
  662. /* default */
  663. vid = PS3AV_DEFAULT_HDMI_VID_REG_60;
  664. }
  665. } else {
  666. if (info->res_50.res_bits & PS3AV_RESBIT_720x576P)
  667. vid = PS3AV_DEFAULT_HDMI_VID_REG_50;
  668. else if (info->res_60.res_bits & PS3AV_RESBIT_720x480P)
  669. vid = PS3AV_DEFAULT_HDMI_VID_REG_60;
  670. else {
  671. /* default */
  672. vid = PS3AV_DEFAULT_HDMI_VID_REG_50;
  673. }
  674. }
  675. }
  676. return (ps3av_vid2table_id(vid) | dvi | rgb);
  677. }
  678. static int ps3av_get_hw_conf(struct ps3av *ps3av)
  679. {
  680. int i, j, k, res;
  681. const struct ps3av_pkt_av_get_hw_conf *hw_conf;
  682. /* get av_hw_conf */
  683. res = ps3av_cmd_av_get_hw_conf(&ps3av->av_hw_conf);
  684. if (res < 0)
  685. return -1;
  686. hw_conf = &ps3av->av_hw_conf;
  687. pr_debug("av_h_conf: num of hdmi: %u\n", hw_conf->num_of_hdmi);
  688. pr_debug("av_h_conf: num of avmulti: %u\n", hw_conf->num_of_avmulti);
  689. pr_debug("av_h_conf: num of spdif: %u\n", hw_conf->num_of_spdif);
  690. for (i = 0; i < PS3AV_HEAD_MAX; i++)
  691. ps3av->head[i] = PS3AV_CMD_VIDEO_HEAD_A + i;
  692. for (i = 0; i < PS3AV_OPT_PORT_MAX; i++)
  693. ps3av->opt_port[i] = PS3AV_CMD_AVPORT_SPDIF_0 + i;
  694. for (i = 0; i < hw_conf->num_of_hdmi; i++)
  695. ps3av->av_port[i] = PS3AV_CMD_AVPORT_HDMI_0 + i;
  696. for (j = 0; j < hw_conf->num_of_avmulti; j++)
  697. ps3av->av_port[i + j] = PS3AV_CMD_AVPORT_AVMULTI_0 + j;
  698. for (k = 0; k < hw_conf->num_of_spdif; k++)
  699. ps3av->av_port[i + j + k] = PS3AV_CMD_AVPORT_SPDIF_0 + k;
  700. /* set all audio port */
  701. ps3av->audio_port = PS3AV_CMD_AUDIO_PORT_HDMI_0
  702. | PS3AV_CMD_AUDIO_PORT_HDMI_1
  703. | PS3AV_CMD_AUDIO_PORT_AVMULTI_0
  704. | PS3AV_CMD_AUDIO_PORT_SPDIF_0 | PS3AV_CMD_AUDIO_PORT_SPDIF_1;
  705. return 0;
  706. }
  707. /* set mode using id */
  708. int ps3av_set_video_mode(u32 id, int boot)
  709. {
  710. int size;
  711. u32 option;
  712. size = ARRAY_SIZE(video_mode_table);
  713. if ((id & PS3AV_MODE_MASK) > size - 1 || id < 0) {
  714. dev_dbg(&ps3av->dev->core, "%s: error id :%d\n", __func__, id);
  715. return -EINVAL;
  716. }
  717. /* auto mode */
  718. option = id & ~PS3AV_MODE_MASK;
  719. if ((id & PS3AV_MODE_MASK) == 0) {
  720. id = ps3av_auto_videomode(&ps3av->av_hw_conf, boot);
  721. if (id < 1) {
  722. printk(KERN_ERR "%s: invalid id :%d\n", __func__, id);
  723. return -EINVAL;
  724. }
  725. id |= option;
  726. }
  727. /* set videomode */
  728. wait_for_completion(&ps3av->done);
  729. ps3av->ps3av_mode_old = ps3av->ps3av_mode;
  730. ps3av->ps3av_mode = id;
  731. if (ps3av_set_videomode())
  732. ps3av->ps3av_mode = ps3av->ps3av_mode_old;
  733. return 0;
  734. }
  735. EXPORT_SYMBOL_GPL(ps3av_set_video_mode);
  736. int ps3av_get_auto_mode(int boot)
  737. {
  738. return ps3av_auto_videomode(&ps3av->av_hw_conf, boot);
  739. }
  740. EXPORT_SYMBOL_GPL(ps3av_get_auto_mode);
  741. int ps3av_set_mode(u32 id, int boot)
  742. {
  743. int res;
  744. res = ps3av_set_video_mode(id, boot);
  745. if (res)
  746. return res;
  747. res = ps3av_set_audio_mode(PS3AV_CMD_AUDIO_NUM_OF_CH_2,
  748. PS3AV_CMD_AUDIO_FS_48K,
  749. PS3AV_CMD_AUDIO_WORD_BITS_16,
  750. PS3AV_CMD_AUDIO_FORMAT_PCM,
  751. PS3AV_CMD_AUDIO_SOURCE_SERIAL);
  752. if (res)
  753. return res;
  754. return 0;
  755. }
  756. EXPORT_SYMBOL_GPL(ps3av_set_mode);
  757. int ps3av_get_mode(void)
  758. {
  759. return ps3av ? ps3av->ps3av_mode : 0;
  760. }
  761. EXPORT_SYMBOL_GPL(ps3av_get_mode);
  762. int ps3av_get_scanmode(int id)
  763. {
  764. int size;
  765. id = id & PS3AV_MODE_MASK;
  766. size = ARRAY_SIZE(video_mode_table);
  767. if (id > size - 1 || id < 0) {
  768. printk(KERN_ERR "%s: invalid mode %d\n", __func__, id);
  769. return -EINVAL;
  770. }
  771. return video_mode_table[id].interlace;
  772. }
  773. EXPORT_SYMBOL_GPL(ps3av_get_scanmode);
  774. int ps3av_get_refresh_rate(int id)
  775. {
  776. int size;
  777. id = id & PS3AV_MODE_MASK;
  778. size = ARRAY_SIZE(video_mode_table);
  779. if (id > size - 1 || id < 0) {
  780. printk(KERN_ERR "%s: invalid mode %d\n", __func__, id);
  781. return -EINVAL;
  782. }
  783. return video_mode_table[id].freq;
  784. }
  785. EXPORT_SYMBOL_GPL(ps3av_get_refresh_rate);
  786. /* get resolution by video_mode */
  787. int ps3av_video_mode2res(u32 id, u32 *xres, u32 *yres)
  788. {
  789. int size;
  790. id = id & PS3AV_MODE_MASK;
  791. size = ARRAY_SIZE(video_mode_table);
  792. if (id > size - 1 || id < 0) {
  793. printk(KERN_ERR "%s: invalid mode %d\n", __func__, id);
  794. return -EINVAL;
  795. }
  796. *xres = video_mode_table[id].x;
  797. *yres = video_mode_table[id].y;
  798. return 0;
  799. }
  800. EXPORT_SYMBOL_GPL(ps3av_video_mode2res);
  801. /* mute */
  802. int ps3av_video_mute(int mute)
  803. {
  804. return ps3av_set_av_video_mute(mute ? PS3AV_CMD_MUTE_ON
  805. : PS3AV_CMD_MUTE_OFF);
  806. }
  807. EXPORT_SYMBOL_GPL(ps3av_video_mute);
  808. int ps3av_audio_mute(int mute)
  809. {
  810. return ps3av_set_audio_mute(mute ? PS3AV_CMD_MUTE_ON
  811. : PS3AV_CMD_MUTE_OFF);
  812. }
  813. EXPORT_SYMBOL_GPL(ps3av_audio_mute);
  814. void ps3av_register_flip_ctl(void (*flip_ctl)(int on, void *data),
  815. void *flip_data)
  816. {
  817. mutex_lock(&ps3av->mutex);
  818. ps3av->flip_ctl = flip_ctl;
  819. ps3av->flip_data = flip_data;
  820. mutex_unlock(&ps3av->mutex);
  821. }
  822. EXPORT_SYMBOL_GPL(ps3av_register_flip_ctl);
  823. void ps3av_flip_ctl(int on)
  824. {
  825. mutex_lock(&ps3av->mutex);
  826. if (ps3av->flip_ctl)
  827. ps3av->flip_ctl(on, ps3av->flip_data);
  828. mutex_unlock(&ps3av->mutex);
  829. }
  830. static int ps3av_probe(struct ps3_system_bus_device *dev)
  831. {
  832. int res;
  833. u32 id;
  834. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  835. dev_dbg(&dev->core, " timeout=%d\n", timeout);
  836. if (ps3av) {
  837. dev_err(&dev->core, "Only one ps3av device is supported\n");
  838. return -EBUSY;
  839. }
  840. ps3av = kzalloc(sizeof(*ps3av), GFP_KERNEL);
  841. if (!ps3av)
  842. return -ENOMEM;
  843. mutex_init(&ps3av->mutex);
  844. ps3av->ps3av_mode = 0;
  845. ps3av->dev = dev;
  846. INIT_WORK(&ps3av->work, ps3avd);
  847. init_completion(&ps3av->done);
  848. complete(&ps3av->done);
  849. ps3av->wq = create_singlethread_workqueue("ps3avd");
  850. if (!ps3av->wq)
  851. goto fail;
  852. switch (ps3_os_area_get_av_multi_out()) {
  853. case PS3_PARAM_AV_MULTI_OUT_NTSC:
  854. ps3av->region = PS3AV_REGION_60;
  855. break;
  856. case PS3_PARAM_AV_MULTI_OUT_PAL_YCBCR:
  857. case PS3_PARAM_AV_MULTI_OUT_SECAM:
  858. ps3av->region = PS3AV_REGION_50;
  859. break;
  860. case PS3_PARAM_AV_MULTI_OUT_PAL_RGB:
  861. ps3av->region = PS3AV_REGION_50 | PS3AV_REGION_RGB;
  862. break;
  863. default:
  864. ps3av->region = PS3AV_REGION_60;
  865. break;
  866. }
  867. /* init avsetting modules */
  868. res = ps3av_cmd_init();
  869. if (res < 0)
  870. printk(KERN_ERR "%s: ps3av_cmd_init failed %d\n", __func__,
  871. res);
  872. ps3av_get_hw_conf(ps3av);
  873. id = ps3av_auto_videomode(&ps3av->av_hw_conf, 1);
  874. mutex_lock(&ps3av->mutex);
  875. ps3av->ps3av_mode = id;
  876. mutex_unlock(&ps3av->mutex);
  877. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  878. return 0;
  879. fail:
  880. kfree(ps3av);
  881. ps3av = NULL;
  882. return -ENOMEM;
  883. }
  884. static int ps3av_remove(struct ps3_system_bus_device *dev)
  885. {
  886. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  887. if (ps3av) {
  888. ps3av_cmd_fin();
  889. if (ps3av->wq)
  890. destroy_workqueue(ps3av->wq);
  891. kfree(ps3av);
  892. ps3av = NULL;
  893. }
  894. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  895. return 0;
  896. }
  897. static void ps3av_shutdown(struct ps3_system_bus_device *dev)
  898. {
  899. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  900. ps3av_remove(dev);
  901. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  902. }
  903. static struct ps3_vuart_port_driver ps3av_driver = {
  904. .core.match_id = PS3_MATCH_ID_AV_SETTINGS,
  905. .core.core.name = "ps3_av",
  906. .probe = ps3av_probe,
  907. .remove = ps3av_remove,
  908. .shutdown = ps3av_shutdown,
  909. };
  910. static int ps3av_module_init(void)
  911. {
  912. int error;
  913. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  914. return -ENODEV;
  915. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  916. error = ps3_vuart_port_driver_register(&ps3av_driver);
  917. if (error) {
  918. printk(KERN_ERR
  919. "%s: ps3_vuart_port_driver_register failed %d\n",
  920. __func__, error);
  921. return error;
  922. }
  923. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  924. return error;
  925. }
  926. static void __exit ps3av_module_exit(void)
  927. {
  928. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  929. ps3_vuart_port_driver_unregister(&ps3av_driver);
  930. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  931. }
  932. subsys_initcall(ps3av_module_init);
  933. module_exit(ps3av_module_exit);
  934. MODULE_LICENSE("GPL v2");
  935. MODULE_DESCRIPTION("PS3 AV Settings Driver");
  936. MODULE_AUTHOR("Sony Computer Entertainment Inc.");
  937. MODULE_ALIAS(PS3_MODULE_ALIAS_AV_SETTINGS);