vizzini.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * vizzini.c
  3. *
  4. * Copyright (c) 2011 Exar Corporation, Inc.
  5. *
  6. * ChangeLog:
  7. * v0.76- Support for 3.0.0 (Ubuntu 11.10) (Removed all Kernel source
  8. * compiler conditions and now the base is Kernel 3.0. Ravi Reddy)
  9. * v0.75- Support for 2.6.38.8 (Ubuntu 11.04) - Added
  10. * .usb_driver = &vizzini_driver.
  11. * v0.74- Support for 2.6.35.22 (Ubuntu 10.10) - Added
  12. * #include <linux/slab.h> to fix kmalloc/kfree error.
  13. * v0.73- Fixed VZIOC_SET_REG (by Ravi Reddy).
  14. * v0.72- Support for 2.6.32.21 (by Ravi Reddy, for Ubuntu 10.04).
  15. * v0.71- Support for 2.6.31.
  16. * v0.5 - Tentative support for compiling with the CentOS 5.1
  17. * kernel (2.6.18-53).
  18. * v0.4 - First version. Lots of stuff lifted from
  19. * cdc-acm.c (credits due to Armin Fuerst, Pavel Machek,
  20. * Johannes Erdfelt, Vojtech Pavlik, David Kubicek) and
  21. * and sierra.c (credit due to Kevin Lloyd).
  22. */
  23. /*
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU General Public License
  35. * along with this program; if not, write to the Free Software
  36. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  37. */
  38. #define DRIVER_VERSION "v.0.76"
  39. #define DRIVER_AUTHOR "Rob Duncan <rob.duncan@exar.com>"
  40. #define DRIVER_DESC "USB Driver for Vizzini USB serial port"
  41. #undef VIZZINI_IWA
  42. #include <linux/kernel.h>
  43. #include <linux/jiffies.h>
  44. #include <linux/errno.h>
  45. #include <linux/tty.h>
  46. #include <linux/tty_flip.h>
  47. #include <linux/module.h>
  48. #include <linux/usb.h>
  49. #include <linux/usb/serial.h>
  50. #include <linux/serial.h>
  51. #include <linux/slab.h>
  52. #include <linux/uaccess.h>
  53. #include <asm/unaligned.h>
  54. #include <linux/usb/cdc.h>
  55. #ifndef CDC_DATA_INTERFACE_TYPE
  56. #define CDC_DATA_INTERFACE_TYPE 0x0a
  57. #endif
  58. #ifndef USB_RT_ACM
  59. #define USB_RT_ACM (USB_TYPE_CLASS | USB_RECIP_INTERFACE)
  60. #define ACM_CTRL_DTR 0x01
  61. #define ACM_CTRL_RTS 0x02
  62. #define ACM_CTRL_DCD 0x01
  63. #define ACM_CTRL_DSR 0x02
  64. #define ACM_CTRL_BRK 0x04
  65. #define ACM_CTRL_RI 0x08
  66. #define ACM_CTRL_FRAMING 0x10
  67. #define ACM_CTRL_PARITY 0x20
  68. #define ACM_CTRL_OVERRUN 0x40
  69. #endif
  70. #define XR_SET_REG 0
  71. #define XR_GETN_REG 1
  72. #define UART_0_REG_BLOCK 0
  73. #define UART_1_REG_BLOCK 1
  74. #define UART_2_REG_BLOCK 2
  75. #define UART_3_REG_BLOCK 3
  76. #define URM_REG_BLOCK 4
  77. #define PRM_REG_BLOCK 5
  78. #define EPMERR_REG_BLOCK 6
  79. #define RAMCTL_REG_BLOCK 0x64
  80. #define TWI_ROM_REG_BLOCK 0x65
  81. #define EPLOCALS_REG_BLOCK 0x66
  82. #define MEM_SHADOW_REG_SIZE_S 5
  83. #define MEM_SHADOW_REG_SIZE (1 << MEM_SHADOW_REG_SIZE_S)
  84. #define MEM_EP_LOCALS_SIZE_S 3
  85. #define MEM_EP_LOCALS_SIZE (1 << MEM_EP_LOCALS_SIZE_S)
  86. #define EP_WIDE_MODE 0x03
  87. #define UART_GPIO_MODE 0x01a
  88. #define UART_GPIO_MODE_SEL_M 0x7
  89. #define UART_GPIO_MODE_SEL_S 0
  90. #define UART_GPIO_MODE_SEL 0x007
  91. #define UART_GPIO_MODE_SEL_GPIO (0x0 << UART_GPIO_MODE_SEL_S)
  92. #define UART_GPIO_MODE_SEL_RTS_CTS (0x1 << UART_GPIO_MODE_SEL_S)
  93. #define UART_GPIO_MODE_SEL_DTR_DSR (0x2 << UART_GPIO_MODE_SEL_S)
  94. #define UART_GPIO_MODE_SEL_XCVR_EN_ACT (0x3 << UART_GPIO_MODE_SEL_S)
  95. #define UART_GPIO_MODE_SEL_XCVR_EN_FLOW (0x4 << UART_GPIO_MODE_SEL_S)
  96. #define UART_GPIO_MODE_XCVR_EN_POL_M 0x1
  97. #define UART_GPIO_MODE_XCVR_EN_POL_S 3
  98. #define UART_GPIO_MODE_XCVR_EN_POL 0x008
  99. #define UART_ENABLE 0x003
  100. #define UART_ENABLE_TX_M 0x1
  101. #define UART_ENABLE_TX_S 0
  102. #define UART_ENABLE_TX 0x001
  103. #define UART_ENABLE_RX_M 0x1
  104. #define UART_ENABLE_RX_S 1
  105. #define UART_ENABLE_RX 0x002
  106. #define UART_CLOCK_DIVISOR_0 0x004
  107. #define UART_CLOCK_DIVISOR_1 0x005
  108. #define UART_CLOCK_DIVISOR_2 0x006
  109. #define UART_CLOCK_DIVISOR_2_MSB_M 0x7
  110. #define UART_CLOCK_DIVISOR_2_MSB_S 0
  111. #define UART_CLOCK_DIVISOR_2_MSB 0x007
  112. #define UART_CLOCK_DIVISOR_2_DIAGMODE_M 0x1
  113. #define UART_CLOCK_DIVISOR_2_DIAGMODE_S 3
  114. #define UART_CLOCK_DIVISOR_2_DIAGMODE 0x008
  115. #define UART_TX_CLOCK_MASK_0 0x007
  116. #define UART_TX_CLOCK_MASK_1 0x008
  117. #define UART_RX_CLOCK_MASK_0 0x009
  118. #define UART_RX_CLOCK_MASK_1 0x00a
  119. #define UART_FORMAT 0x00b
  120. #define UART_FORMAT_SIZE_M 0xf
  121. #define UART_FORMAT_SIZE_S 0
  122. #define UART_FORMAT_SIZE 0x00f
  123. #define UART_FORMAT_SIZE_7 (0x7 << UART_FORMAT_SIZE_S)
  124. #define UART_FORMAT_SIZE_8 (0x8 << UART_FORMAT_SIZE_S)
  125. #define UART_FORMAT_SIZE_9 (0x9 << UART_FORMAT_SIZE_S)
  126. #define UART_FORMAT_PARITY_M 0x7
  127. #define UART_FORMAT_PARITY_S 4
  128. #define UART_FORMAT_PARITY 0x070
  129. #define UART_FORMAT_PARITY_NONE (0x0 << UART_FORMAT_PARITY_S)
  130. #define UART_FORMAT_PARITY_ODD (0x1 << UART_FORMAT_PARITY_S)
  131. #define UART_FORMAT_PARITY_EVEN (0x2 << UART_FORMAT_PARITY_S)
  132. #define UART_FORMAT_PARITY_1 (0x3 << UART_FORMAT_PARITY_S)
  133. #define UART_FORMAT_PARITY_0 (0x4 << UART_FORMAT_PARITY_S)
  134. #define UART_FORMAT_STOP_M 0x1
  135. #define UART_FORMAT_STOP_S 7
  136. #define UART_FORMAT_STOP 0x080
  137. #define UART_FORMAT_STOP_1 (0x0 << UART_FORMAT_STOP_S)
  138. #define UART_FORMAT_STOP_2 (0x1 << UART_FORMAT_STOP_S)
  139. #define UART_FORMAT_MODE_7N1 0
  140. #define UART_FORMAT_MODE_RES1 1
  141. #define UART_FORMAT_MODE_RES2 2
  142. #define UART_FORMAT_MODE_RES3 3
  143. #define UART_FORMAT_MODE_7N2 4
  144. #define UART_FORMAT_MODE_7P1 5
  145. #define UART_FORMAT_MODE_8N1 6
  146. #define UART_FORMAT_MODE_RES7 7
  147. #define UART_FORMAT_MODE_7P2 8
  148. #define UART_FORMAT_MODE_8N2 9
  149. #define UART_FORMAT_MODE_8P1 10
  150. #define UART_FORMAT_MODE_9N1 11
  151. #define UART_FORMAT_MODE_8P2 12
  152. #define UART_FORMAT_MODE_RESD 13
  153. #define UART_FORMAT_MODE_RESE 14
  154. #define UART_FORMAT_MODE_9N2 15
  155. #define UART_FLOW 0x00c
  156. #define UART_FLOW_MODE_M 0x7
  157. #define UART_FLOW_MODE_S 0
  158. #define UART_FLOW_MODE 0x007
  159. #define UART_FLOW_MODE_NONE (0x0 << UART_FLOW_MODE_S)
  160. #define UART_FLOW_MODE_HW (0x1 << UART_FLOW_MODE_S)
  161. #define UART_FLOW_MODE_SW (0x2 << UART_FLOW_MODE_S)
  162. #define UART_FLOW_MODE_ADDR_MATCH (0x3 << UART_FLOW_MODE_S)
  163. #define UART_FLOW_MODE_ADDR_MATCH_TX (0x4 << UART_FLOW_MODE_S)
  164. #define UART_FLOW_HALF_DUPLEX_M 0x1
  165. #define UART_FLOW_HALF_DUPLEX_S 3
  166. #define UART_FLOW_HALF_DUPLEX 0x008
  167. #define UART_LOOPBACK_CTL 0x012
  168. #define UART_LOOPBACK_CTL_ENABLE_M 0x1
  169. #define UART_LOOPBACK_CTL_ENABLE_S 2
  170. #define UART_LOOPBACK_CTL_ENABLE 0x004
  171. #define UART_LOOPBACK_CTL_RX_SOURCE_M 0x3
  172. #define UART_LOOPBACK_CTL_RX_SOURCE_S 0
  173. #define UART_LOOPBACK_CTL_RX_SOURCE 0x003
  174. #define UART_LOOPBACK_CTL_RX_UART0 (0x0 << UART_LOOPBACK_CTL_RX_SOURCE_S)
  175. #define UART_LOOPBACK_CTL_RX_UART1 (0x1 << UART_LOOPBACK_CTL_RX_SOURCE_S)
  176. #define UART_LOOPBACK_CTL_RX_UART2 (0x2 << UART_LOOPBACK_CTL_RX_SOURCE_S)
  177. #define UART_LOOPBACK_CTL_RX_UART3 (0x3 << UART_LOOPBACK_CTL_RX_SOURCE_S)
  178. #define UART_CHANNEL_NUM 0x00d
  179. #define UART_XON_CHAR 0x010
  180. #define UART_XOFF_CHAR 0x011
  181. #define UART_GPIO_SET 0x01d
  182. #define UART_GPIO_CLR 0x01e
  183. #define UART_GPIO_STATUS 0x01f
  184. #define URM_ENABLE_BASE 0x010
  185. #define URM_ENABLE_0 0x010
  186. #define URM_ENABLE_0_TX_M 0x1
  187. #define URM_ENABLE_0_TX_S 0
  188. #define URM_ENABLE_0_TX 0x001
  189. #define URM_ENABLE_0_RX_M 0x1
  190. #define URM_ENABLE_0_RX_S 1
  191. #define URM_ENABLE_0_RX 0x002
  192. #define URM_RX_FIFO_RESET_0 0x018
  193. #define URM_RX_FIFO_RESET_1 0x019
  194. #define URM_RX_FIFO_RESET_2 0x01a
  195. #define URM_RX_FIFO_RESET_3 0x01b
  196. #define URM_TX_FIFO_RESET_0 0x01c
  197. #define URM_TX_FIFO_RESET_1 0x01d
  198. #define URM_TX_FIFO_RESET_2 0x01e
  199. #define URM_TX_FIFO_RESET_3 0x01f
  200. #define RAMCTL_REGS_TXFIFO_0_LEVEL 0x000
  201. #define RAMCTL_REGS_TXFIFO_1_LEVEL 0x001
  202. #define RAMCTL_REGS_TXFIFO_2_LEVEL 0x002
  203. #define RAMCTL_REGS_TXFIFO_3_LEVEL 0x003
  204. #define RAMCTL_REGS_RXFIFO_0_LEVEL 0x004
  205. #define RAMCTL_REGS_RXFIFO_0_LEVEL_LEVEL_M 0x7ff
  206. #define RAMCTL_REGS_RXFIFO_0_LEVEL_LEVEL_S 0
  207. #define RAMCTL_REGS_RXFIFO_0_LEVEL_LEVEL 0x7ff
  208. #define RAMCTL_REGS_RXFIFO_0_LEVEL_STALE_M 0x1
  209. #define RAMCTL_REGS_RXFIFO_0_LEVEL_STALE_S 11
  210. #define RAMCTL_REGS_RXFIFO_0_LEVEL_STALE 0x800
  211. #define RAMCTL_REGS_RXFIFO_1_LEVEL 0x005
  212. #define RAMCTL_REGS_RXFIFO_2_LEVEL 0x006
  213. #define RAMCTL_REGS_RXFIFO_3_LEVEL 0x007
  214. #define RAMCTL_BUFFER_PARITY 0x1
  215. #define RAMCTL_BUFFER_BREAK 0x2
  216. #define RAMCTL_BUFFER_FRAME 0x4
  217. #define RAMCTL_BUFFER_OVERRUN 0x8
  218. #define N_IN_URB 4
  219. #define N_OUT_URB 4
  220. #define IN_BUFLEN 4096
  221. static struct usb_device_id id_table[] = {
  222. { USB_DEVICE(0x04e2, 0x1410) },
  223. { USB_DEVICE(0x04e2, 0x1412) },
  224. { USB_DEVICE(0x04e2, 0x1414) },
  225. { }
  226. };
  227. MODULE_DEVICE_TABLE(usb, id_table);
  228. struct vizzini_serial_private {
  229. struct usb_interface *data_interface;
  230. };
  231. struct vizzini_port_private {
  232. spinlock_t lock;
  233. int outstanding_urbs;
  234. struct urb *in_urbs[N_IN_URB];
  235. char *in_buffer[N_IN_URB];
  236. int ctrlin;
  237. int ctrlout;
  238. int clocal;
  239. int block;
  240. int preciseflags; /* USB: wide mode, TTY: flags per character */
  241. int trans9; /* USB: wide mode, serial 9N1 */
  242. unsigned int baud_base; /* setserial: used to hack in non-standard baud rates */
  243. int have_extra_byte;
  244. int extra_byte;
  245. int bcd_device;
  246. #ifdef VIZZINI_IWA
  247. int iwa;
  248. #endif
  249. };
  250. static int vizzini_rev_a(struct usb_serial_port *port)
  251. {
  252. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  253. return portdata->bcd_device == 0;
  254. }
  255. static int acm_ctrl_msg(struct usb_serial_port *port, int request,
  256. int value, void *buf, int len)
  257. {
  258. struct usb_serial *serial = port->serial;
  259. int retval = usb_control_msg(serial->dev,
  260. usb_sndctrlpipe(serial->dev, 0),
  261. request,
  262. USB_RT_ACM,
  263. value,
  264. serial->interface->cur_altsetting->desc.bInterfaceNumber,
  265. buf,
  266. len,
  267. 5000);
  268. dev_dbg(&port->dev, "acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d\n", request, value, len, retval);
  269. return retval < 0 ? retval : 0;
  270. }
  271. #define acm_set_control(port, control) \
  272. acm_ctrl_msg(port, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
  273. #define acm_set_line(port, line) \
  274. acm_ctrl_msg(port, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
  275. #define acm_send_break(port, ms) \
  276. acm_ctrl_msg(port, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
  277. static int vizzini_set_reg(struct usb_serial_port *port, int block, int regnum, int value)
  278. {
  279. struct usb_serial *serial = port->serial;
  280. int result;
  281. result = usb_control_msg(serial->dev, /* usb device */
  282. usb_sndctrlpipe(serial->dev, 0), /* endpoint pipe */
  283. XR_SET_REG, /* request */
  284. USB_DIR_OUT | USB_TYPE_VENDOR, /* request_type */
  285. value, /* request value */
  286. regnum | (block << 8), /* index */
  287. NULL, /* data */
  288. 0, /* size */
  289. 5000); /* timeout */
  290. return result;
  291. }
  292. static void vizzini_disable(struct usb_serial_port *port)
  293. {
  294. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  295. int block = portdata->block;
  296. vizzini_set_reg(port, block, UART_ENABLE, 0);
  297. vizzini_set_reg(port, URM_REG_BLOCK, URM_ENABLE_BASE + block, 0);
  298. }
  299. static void vizzini_enable(struct usb_serial_port *port)
  300. {
  301. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  302. int block = portdata->block;
  303. vizzini_set_reg(port, URM_REG_BLOCK, URM_ENABLE_BASE + block, URM_ENABLE_0_TX);
  304. vizzini_set_reg(port, block, UART_ENABLE, UART_ENABLE_TX | UART_ENABLE_RX);
  305. vizzini_set_reg(port, URM_REG_BLOCK, URM_ENABLE_BASE + block, URM_ENABLE_0_TX | URM_ENABLE_0_RX);
  306. }
  307. struct vizzini_baud_rate {
  308. unsigned int tx;
  309. unsigned int rx0;
  310. unsigned int rx1;
  311. };
  312. static struct vizzini_baud_rate vizzini_baud_rates[] = {
  313. { 0x000, 0x000, 0x000 },
  314. { 0x000, 0x000, 0x000 },
  315. { 0x100, 0x000, 0x100 },
  316. { 0x020, 0x400, 0x020 },
  317. { 0x010, 0x100, 0x010 },
  318. { 0x208, 0x040, 0x208 },
  319. { 0x104, 0x820, 0x108 },
  320. { 0x844, 0x210, 0x884 },
  321. { 0x444, 0x110, 0x444 },
  322. { 0x122, 0x888, 0x224 },
  323. { 0x912, 0x448, 0x924 },
  324. { 0x492, 0x248, 0x492 },
  325. { 0x252, 0x928, 0x292 },
  326. { 0X94A, 0X4A4, 0XA52 },
  327. { 0X52A, 0XAA4, 0X54A },
  328. { 0XAAA, 0x954, 0X4AA },
  329. { 0XAAA, 0x554, 0XAAA },
  330. { 0x555, 0XAD4, 0X5AA },
  331. { 0XB55, 0XAB4, 0X55A },
  332. { 0X6B5, 0X5AC, 0XB56 },
  333. { 0X5B5, 0XD6C, 0X6D6 },
  334. { 0XB6D, 0XB6A, 0XDB6 },
  335. { 0X76D, 0X6DA, 0XBB6 },
  336. { 0XEDD, 0XDDA, 0X76E },
  337. { 0XDDD, 0XBBA, 0XEEE },
  338. { 0X7BB, 0XF7A, 0XDDE },
  339. { 0XF7B, 0XEF6, 0X7DE },
  340. { 0XDF7, 0XBF6, 0XF7E },
  341. { 0X7F7, 0XFEE, 0XEFE },
  342. { 0XFDF, 0XFBE, 0X7FE },
  343. { 0XF7F, 0XEFE, 0XFFE },
  344. { 0XFFF, 0XFFE, 0XFFD },
  345. };
  346. static int vizzini_set_baud_rate(struct usb_serial_port *port, unsigned int rate)
  347. {
  348. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  349. int block = portdata->block;
  350. unsigned int divisor = 48000000 / rate;
  351. unsigned int i = ((32 * 48000000) / rate) & 0x1f;
  352. unsigned int tx_mask = vizzini_baud_rates[i].tx;
  353. unsigned int rx_mask = (divisor & 1) ? vizzini_baud_rates[i].rx1 : vizzini_baud_rates[i].rx0;
  354. dev_dbg(&port->dev, "Setting baud rate to %d: i=%u div=%u tx=%03x rx=%03x\n", rate, i, divisor, tx_mask, rx_mask);
  355. vizzini_set_reg(port, block, UART_CLOCK_DIVISOR_0, (divisor >> 0) & 0xff);
  356. vizzini_set_reg(port, block, UART_CLOCK_DIVISOR_1, (divisor >> 8) & 0xff);
  357. vizzini_set_reg(port, block, UART_CLOCK_DIVISOR_2, (divisor >> 16) & 0xff);
  358. vizzini_set_reg(port, block, UART_TX_CLOCK_MASK_0, (tx_mask >> 0) & 0xff);
  359. vizzini_set_reg(port, block, UART_TX_CLOCK_MASK_1, (tx_mask >> 8) & 0xff);
  360. vizzini_set_reg(port, block, UART_RX_CLOCK_MASK_0, (rx_mask >> 0) & 0xff);
  361. vizzini_set_reg(port, block, UART_RX_CLOCK_MASK_1, (rx_mask >> 8) & 0xff);
  362. return -EINVAL;
  363. }
  364. static void vizzini_set_termios(struct tty_struct *tty_param,
  365. struct usb_serial_port *port,
  366. struct ktermios *old_termios)
  367. {
  368. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  369. unsigned int cflag, block;
  370. speed_t rate;
  371. unsigned int format_size, format_parity, format_stop, flow, gpio_mode;
  372. struct tty_struct *tty = port->port.tty;
  373. cflag = tty->termios->c_cflag;
  374. portdata->clocal = ((cflag & CLOCAL) != 0);
  375. block = portdata->block;
  376. vizzini_disable(port);
  377. if ((cflag & CSIZE) == CS7) {
  378. format_size = UART_FORMAT_SIZE_7;
  379. } else if ((cflag & CSIZE) == CS5) {
  380. /* Enabling 5-bit mode is really 9-bit mode! */
  381. format_size = UART_FORMAT_SIZE_9;
  382. } else {
  383. format_size = UART_FORMAT_SIZE_8;
  384. }
  385. portdata->trans9 = (format_size == UART_FORMAT_SIZE_9);
  386. if (cflag & PARENB) {
  387. if (cflag & PARODD) {
  388. if (cflag & CMSPAR)
  389. format_parity = UART_FORMAT_PARITY_1;
  390. else
  391. format_parity = UART_FORMAT_PARITY_ODD;
  392. } else {
  393. if (cflag & CMSPAR)
  394. format_parity = UART_FORMAT_PARITY_0;
  395. else
  396. format_parity = UART_FORMAT_PARITY_EVEN;
  397. }
  398. } else {
  399. format_parity = UART_FORMAT_PARITY_NONE;
  400. }
  401. if (cflag & CSTOPB)
  402. format_stop = UART_FORMAT_STOP_2;
  403. else
  404. format_stop = UART_FORMAT_STOP_1;
  405. #ifdef VIZZINI_IWA
  406. if (format_size == UART_FORMAT_SIZE_8) {
  407. portdata->iwa = format_parity;
  408. if (portdata->iwa != UART_FORMAT_PARITY_NONE) {
  409. format_size = UART_FORMAT_SIZE_9;
  410. format_parity = UART_FORMAT_PARITY_NONE;
  411. }
  412. } else {
  413. portdata->iwa = UART_FORMAT_PARITY_NONE;
  414. }
  415. #endif
  416. vizzini_set_reg(port, block, UART_FORMAT, format_size | format_parity | format_stop);
  417. if (cflag & CRTSCTS) {
  418. flow = UART_FLOW_MODE_HW;
  419. gpio_mode = UART_GPIO_MODE_SEL_RTS_CTS;
  420. } else if (I_IXOFF(tty) || I_IXON(tty)) {
  421. unsigned char start_char = START_CHAR(tty);
  422. unsigned char stop_char = STOP_CHAR(tty);
  423. flow = UART_FLOW_MODE_SW;
  424. gpio_mode = UART_GPIO_MODE_SEL_GPIO;
  425. vizzini_set_reg(port, block, UART_XON_CHAR, start_char);
  426. vizzini_set_reg(port, block, UART_XOFF_CHAR, stop_char);
  427. } else {
  428. flow = UART_FLOW_MODE_NONE;
  429. gpio_mode = UART_GPIO_MODE_SEL_GPIO;
  430. }
  431. vizzini_set_reg(port, block, UART_FLOW, flow);
  432. vizzini_set_reg(port, block, UART_GPIO_MODE, gpio_mode);
  433. if (portdata->trans9) {
  434. /* Turn on wide mode if we're 9-bit transparent. */
  435. vizzini_set_reg(port, EPLOCALS_REG_BLOCK, (block * MEM_EP_LOCALS_SIZE) + EP_WIDE_MODE, 1);
  436. #ifdef VIZZINI_IWA
  437. } else if (portdata->iwa != UART_FORMAT_PARITY_NONE) {
  438. vizzini_set_reg(port, EPLOCALS_REG_BLOCK, (block * MEM_EP_LOCALS_SIZE) + EP_WIDE_MODE, 1);
  439. #endif
  440. } else if (!portdata->preciseflags) {
  441. /* Turn off wide mode unless we have precise flags. */
  442. vizzini_set_reg(port, EPLOCALS_REG_BLOCK, (block * MEM_EP_LOCALS_SIZE) + EP_WIDE_MODE, 0);
  443. }
  444. rate = tty_get_baud_rate(tty);
  445. if (rate)
  446. vizzini_set_baud_rate(port, rate);
  447. vizzini_enable(port);
  448. }
  449. static void vizzini_break_ctl(struct tty_struct *tty, int break_state)
  450. {
  451. struct usb_serial_port *port = tty->driver_data;
  452. dev_dbg(&port->dev, "BREAK %d\n", break_state);
  453. if (break_state)
  454. acm_send_break(port, 0x10);
  455. else
  456. acm_send_break(port, 0x000);
  457. }
  458. static int vizzini_tiocmget(struct tty_struct *tty)
  459. {
  460. struct usb_serial_port *port = tty->driver_data;
  461. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  462. return (portdata->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  463. (portdata->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  464. (portdata->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  465. (portdata->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  466. (portdata->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  467. TIOCM_CTS;
  468. }
  469. static int vizzini_tiocmset(struct tty_struct *tty,
  470. unsigned int set, unsigned int clear)
  471. {
  472. struct usb_serial_port *port = tty->driver_data;
  473. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  474. unsigned int newctrl;
  475. newctrl = portdata->ctrlout;
  476. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  477. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  478. newctrl = (newctrl & ~clear) | set;
  479. if (portdata->ctrlout == newctrl)
  480. return 0;
  481. return acm_set_control(port, portdata->ctrlout = newctrl);
  482. }
  483. static int vizzini_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
  484. {
  485. struct usb_serial_port *port = tty->driver_data;
  486. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  487. struct serial_struct ss;
  488. dev_dbg(&port->dev, "%s %08x\n", __func__, cmd);
  489. switch (cmd) {
  490. case TIOCGSERIAL:
  491. if (!arg)
  492. return -EFAULT;
  493. memset(&ss, 0, sizeof(ss));
  494. ss.baud_base = portdata->baud_base;
  495. if (copy_to_user((void __user *)arg, &ss, sizeof(ss)))
  496. return -EFAULT;
  497. break;
  498. case TIOCSSERIAL:
  499. if (!arg)
  500. return -EFAULT;
  501. if (copy_from_user(&ss, (void __user *)arg, sizeof(ss)))
  502. return -EFAULT;
  503. portdata->baud_base = ss.baud_base;
  504. dev_dbg(&port->dev, "baud_base=%d\n", portdata->baud_base);
  505. vizzini_disable(port);
  506. if (portdata->baud_base)
  507. vizzini_set_baud_rate(port, portdata->baud_base);
  508. vizzini_enable(port);
  509. break;
  510. default:
  511. return -ENOIOCTLCMD;
  512. }
  513. return 0;
  514. }
  515. #ifdef VIZZINI_IWA
  516. static const int vizzini_parity[] = {
  517. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  518. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  519. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  520. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  521. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  522. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  523. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  524. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  525. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  526. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  527. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  528. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  529. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  530. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  531. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  532. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
  533. };
  534. #endif
  535. static void vizzini_out_callback(struct urb *urb)
  536. {
  537. struct usb_serial_port *port = urb->context;
  538. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  539. int status = urb->status;
  540. unsigned long flags;
  541. dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
  542. /* free up the transfer buffer, as usb_free_urb() does not do this */
  543. kfree(urb->transfer_buffer);
  544. if (status)
  545. dev_dbg(&port->dev, "%s - nonzero write bulk status received: %d\n", __func__, status);
  546. spin_lock_irqsave(&portdata->lock, flags);
  547. --portdata->outstanding_urbs;
  548. spin_unlock_irqrestore(&portdata->lock, flags);
  549. usb_serial_port_softint(port);
  550. }
  551. static int vizzini_write_room(struct tty_struct *tty)
  552. {
  553. struct usb_serial_port *port = tty->driver_data;
  554. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  555. unsigned long flags;
  556. dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
  557. /* try to give a good number back based on if we have any free urbs at
  558. * this point in time */
  559. spin_lock_irqsave(&portdata->lock, flags);
  560. if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
  561. spin_unlock_irqrestore(&portdata->lock, flags);
  562. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  563. return 0;
  564. }
  565. spin_unlock_irqrestore(&portdata->lock, flags);
  566. return 2048;
  567. }
  568. static int vizzini_write(struct tty_struct *tty, struct usb_serial_port *port,
  569. const unsigned char *buf, int count)
  570. {
  571. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  572. struct usb_serial *serial = port->serial;
  573. int bufsize = count;
  574. unsigned long flags;
  575. unsigned char *buffer;
  576. struct urb *urb;
  577. int status;
  578. portdata = usb_get_serial_port_data(port);
  579. dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
  580. spin_lock_irqsave(&portdata->lock, flags);
  581. if (portdata->outstanding_urbs > N_OUT_URB) {
  582. spin_unlock_irqrestore(&portdata->lock, flags);
  583. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  584. return 0;
  585. }
  586. portdata->outstanding_urbs++;
  587. spin_unlock_irqrestore(&portdata->lock, flags);
  588. #ifdef VIZZINI_IWA
  589. if (portdata->iwa != UART_FORMAT_PARITY_NONE)
  590. bufsize = count * 2;
  591. #endif
  592. buffer = kmalloc(bufsize, GFP_ATOMIC);
  593. if (!buffer) {
  594. dev_err(&port->dev, "out of memory\n");
  595. count = -ENOMEM;
  596. goto error_no_buffer;
  597. }
  598. urb = usb_alloc_urb(0, GFP_ATOMIC);
  599. if (!urb) {
  600. dev_err(&port->dev, "no more free urbs\n");
  601. count = -ENOMEM;
  602. goto error_no_urb;
  603. }
  604. #ifdef VIZZINI_IWA
  605. if (portdata->iwa != UART_FORMAT_PARITY_NONE) {
  606. int i;
  607. char *b = buffer;
  608. for (i = 0; i < count; ++i) {
  609. int c, p = 0;
  610. c = buf[i];
  611. switch (portdata->iwa) {
  612. case UART_FORMAT_PARITY_ODD:
  613. p = !vizzini_parity[c];
  614. break;
  615. case UART_FORMAT_PARITY_EVEN:
  616. p = vizzini_parity[c];
  617. break;
  618. case UART_FORMAT_PARITY_1:
  619. p = 1;
  620. break;
  621. case UART_FORMAT_PARITY_0:
  622. p = 0;
  623. break;
  624. }
  625. *b++ = c;
  626. *b++ = p;
  627. }
  628. } else
  629. #endif
  630. memcpy(buffer, buf, count);
  631. usb_fill_bulk_urb(urb, serial->dev,
  632. usb_sndbulkpipe(serial->dev,
  633. port->bulk_out_endpointAddress),
  634. buffer, bufsize, vizzini_out_callback, port);
  635. /* send it down the pipe */
  636. status = usb_submit_urb(urb, GFP_ATOMIC);
  637. if (status) {
  638. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__, status);
  639. count = status;
  640. goto error;
  641. }
  642. /* we are done with this urb, so let the host driver
  643. * really free it when it is finished with it */
  644. usb_free_urb(urb);
  645. return count;
  646. error:
  647. usb_free_urb(urb);
  648. error_no_urb:
  649. kfree(buffer);
  650. error_no_buffer:
  651. spin_lock_irqsave(&portdata->lock, flags);
  652. --portdata->outstanding_urbs;
  653. spin_unlock_irqrestore(&portdata->lock, flags);
  654. return count;
  655. }
  656. static void vizzini_in_callback(struct urb *urb)
  657. {
  658. int endpoint = usb_pipeendpoint(urb->pipe);
  659. struct usb_serial_port *port = urb->context;
  660. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  661. struct tty_struct *tty = port->port.tty;
  662. int preciseflags = portdata->preciseflags;
  663. char *transfer_buffer = urb->transfer_buffer;
  664. int length, room, have_extra_byte;
  665. int err;
  666. if (urb->status) {
  667. dev_dbg(&port->dev, "%s: nonzero status: %d on endpoint %02x.\n", __func__, urb->status, endpoint);
  668. return;
  669. }
  670. #ifdef VIZZINI_IWA
  671. if (portdata->iwa != UART_FORMAT_PARITY_NONE)
  672. preciseflags = true;
  673. #endif
  674. length = urb->actual_length;
  675. if (length == 0) {
  676. dev_dbg(&port->dev, "%s: empty read urb received\n", __func__);
  677. err = usb_submit_urb(urb, GFP_ATOMIC);
  678. if (err)
  679. dev_err(&port->dev, "resubmit read urb failed. (%d)\n", err);
  680. return;
  681. }
  682. length = length + (portdata->have_extra_byte ? 1 : 0);
  683. have_extra_byte = (preciseflags && (length & 1));
  684. length = (preciseflags) ? (length / 2) : length;
  685. room = tty_buffer_request_room(tty, length);
  686. if (room != length)
  687. dev_dbg(&port->dev, "Not enough room in TTY buf, dropped %d chars.\n", length - room);
  688. if (room) {
  689. if (preciseflags) {
  690. char *dp = transfer_buffer;
  691. int i, ch, ch_flags;
  692. for (i = 0; i < room; ++i) {
  693. char tty_flag;
  694. if (i == 0) {
  695. if (portdata->have_extra_byte)
  696. ch = portdata->extra_byte;
  697. else
  698. ch = *dp++;
  699. } else {
  700. ch = *dp++;
  701. }
  702. ch_flags = *dp++;
  703. #ifdef VIZZINI_IWA
  704. {
  705. int p;
  706. switch (portdata->iwa) {
  707. case UART_FORMAT_PARITY_ODD:
  708. p = !vizzini_parity[ch];
  709. break;
  710. case UART_FORMAT_PARITY_EVEN:
  711. p = vizzini_parity[ch];
  712. break;
  713. case UART_FORMAT_PARITY_1:
  714. p = 1;
  715. break;
  716. case UART_FORMAT_PARITY_0:
  717. p = 0;
  718. break;
  719. default:
  720. p = 0;
  721. break;
  722. }
  723. ch_flags ^= p;
  724. }
  725. #endif
  726. if (ch_flags & RAMCTL_BUFFER_PARITY)
  727. tty_flag = TTY_PARITY;
  728. else if (ch_flags & RAMCTL_BUFFER_BREAK)
  729. tty_flag = TTY_BREAK;
  730. else if (ch_flags & RAMCTL_BUFFER_FRAME)
  731. tty_flag = TTY_FRAME;
  732. else if (ch_flags & RAMCTL_BUFFER_OVERRUN)
  733. tty_flag = TTY_OVERRUN;
  734. else
  735. tty_flag = TTY_NORMAL;
  736. tty_insert_flip_char(tty, ch, tty_flag);
  737. }
  738. } else {
  739. tty_insert_flip_string(tty, transfer_buffer, room);
  740. }
  741. tty_flip_buffer_push(tty);
  742. }
  743. portdata->have_extra_byte = have_extra_byte;
  744. if (have_extra_byte)
  745. portdata->extra_byte = transfer_buffer[urb->actual_length - 1];
  746. err = usb_submit_urb(urb, GFP_ATOMIC);
  747. if (err)
  748. dev_err(&port->dev, "resubmit read urb failed. (%d)\n", err);
  749. }
  750. static void vizzini_int_callback(struct urb *urb)
  751. {
  752. struct usb_serial_port *port = urb->context;
  753. struct vizzini_port_private *portdata = usb_get_serial_port_data(port);
  754. struct tty_struct *tty = port->port.tty;
  755. struct usb_cdc_notification *dr = urb->transfer_buffer;
  756. unsigned char *data;
  757. int newctrl;
  758. int status;
  759. switch (urb->status) {
  760. case 0:
  761. /* success */
  762. break;
  763. case -ECONNRESET:
  764. case -ENOENT:
  765. case -ESHUTDOWN:
  766. /* this urb is terminated, clean up */
  767. dev_dbg(&port->dev, "urb shutting down with status: %d\n", urb->status);
  768. return;
  769. default:
  770. dev_dbg(&port->dev, "nonzero urb status received: %d\n", urb->status);
  771. goto exit;
  772. }
  773. data = (unsigned char *)(dr + 1);
  774. switch (dr->bNotificationType) {
  775. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  776. dev_dbg(&port->dev, "%s network\n", dr->wValue ? "connected to" : "disconnected from");
  777. break;
  778. case USB_CDC_NOTIFY_SERIAL_STATE:
  779. newctrl = le16_to_cpu(get_unaligned((__le16 *)data));
  780. if (!portdata->clocal && (portdata->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  781. dev_dbg(&port->dev, "calling hangup\n");
  782. tty_hangup(tty);
  783. }
  784. portdata->ctrlin = newctrl;
  785. dev_dbg(&port->dev, "input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c\n",
  786. portdata->ctrlin & ACM_CTRL_DCD ? '+' : '-',
  787. portdata->ctrlin & ACM_CTRL_DSR ? '+' : '-',
  788. portdata->ctrlin & ACM_CTRL_BRK ? '+' : '-',
  789. portdata->ctrlin & ACM_CTRL_RI ? '+' : '-',
  790. portdata->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
  791. portdata->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
  792. portdata->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
  793. break;
  794. default:
  795. dev_dbg(&port->dev, "unknown notification %d received: index %d len %d data0 %d data1 %d\n",
  796. dr->bNotificationType, dr->wIndex,
  797. dr->wLength, data[0], data[1]);
  798. break;
  799. }
  800. exit:
  801. dev_dbg(&port->dev, "Resubmitting interrupt IN urb %p\n", urb);
  802. status = usb_submit_urb(urb, GFP_ATOMIC);
  803. if (status)
  804. dev_err(&port->dev, "usb_submit_urb failed with result %d", status);
  805. }
  806. static int vizzini_open(struct tty_struct *tty_param, struct usb_serial_port *port)
  807. {
  808. struct vizzini_port_private *portdata;
  809. struct usb_serial *serial = port->serial;
  810. struct tty_struct *tty = port->port.tty;
  811. int i;
  812. struct urb *urb;
  813. int result;
  814. portdata = usb_get_serial_port_data(port);
  815. acm_set_control(port, portdata->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS);
  816. /* Reset low level data toggle and start reading from endpoints */
  817. for (i = 0; i < N_IN_URB; i++) {
  818. dev_dbg(&port->dev, "%s urb %d\n", __func__, i);
  819. urb = portdata->in_urbs[i];
  820. if (!urb)
  821. continue;
  822. if (urb->dev != serial->dev) {
  823. dev_dbg(&port->dev, "%s: dev %p != %p\n", __func__,
  824. urb->dev, serial->dev);
  825. continue;
  826. }
  827. /*
  828. * make sure endpoint data toggle is synchronized with the
  829. * device
  830. */
  831. /* dev_dbg(&port->dev, "%s clearing halt on %x\n", __func__, urb->pipe); */
  832. /* usb_clear_halt(urb->dev, urb->pipe); */
  833. dev_dbg(&port->dev, "%s submitting urb %p\n", __func__, urb);
  834. result = usb_submit_urb(urb, GFP_KERNEL);
  835. if (result) {
  836. dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
  837. i, result, urb->transfer_buffer_length);
  838. }
  839. }
  840. tty->low_latency = 1;
  841. /* start up the interrupt endpoint if we have one */
  842. if (port->interrupt_in_urb) {
  843. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  844. if (result)
  845. dev_err(&port->dev, "submit irq_in urb failed %d\n",
  846. result);
  847. }
  848. return 0;
  849. }
  850. static void vizzini_close(struct usb_serial_port *port)
  851. {
  852. int i;
  853. struct usb_serial *serial = port->serial;
  854. struct vizzini_port_private *portdata;
  855. struct tty_struct *tty = port->port.tty;
  856. portdata = usb_get_serial_port_data(port);
  857. acm_set_control(port, portdata->ctrlout = 0);
  858. if (serial->dev) {
  859. /* Stop reading/writing urbs */
  860. for (i = 0; i < N_IN_URB; i++)
  861. usb_kill_urb(portdata->in_urbs[i]);
  862. }
  863. usb_kill_urb(port->interrupt_in_urb);
  864. tty = NULL; /* FIXME */
  865. }
  866. static int vizzini_attach(struct usb_serial *serial)
  867. {
  868. struct vizzini_serial_private *serial_priv = usb_get_serial_data(serial);
  869. struct usb_interface *interface = serial_priv->data_interface;
  870. struct usb_host_interface *iface_desc;
  871. struct usb_endpoint_descriptor *endpoint;
  872. struct usb_endpoint_descriptor *bulk_in_endpoint = NULL;
  873. struct usb_endpoint_descriptor *bulk_out_endpoint = NULL;
  874. struct usb_serial_port *port;
  875. struct vizzini_port_private *portdata;
  876. struct urb *urb;
  877. int i, j;
  878. /* Assume that there's exactly one serial port. */
  879. port = serial->port[0];
  880. /* The usb_serial is now fully set up, but we want to make a
  881. * couple of modifications. Namely, it was configured based
  882. * upon the control interface and not the data interface, so
  883. * it has no notion of the bulk in and out endpoints. So we
  884. * essentially do some of the same allocations and
  885. * configurations that the usb-serial core would have done if
  886. * it had not made any faulty assumptions about the
  887. * endpoints. */
  888. iface_desc = interface->cur_altsetting;
  889. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  890. endpoint = &iface_desc->endpoint[i].desc;
  891. if (usb_endpoint_is_bulk_in(endpoint))
  892. bulk_in_endpoint = endpoint;
  893. if (usb_endpoint_is_bulk_out(endpoint))
  894. bulk_out_endpoint = endpoint;
  895. }
  896. if (!bulk_out_endpoint || !bulk_in_endpoint) {
  897. dev_dbg(&port->dev, "Missing endpoint!\n");
  898. return -EINVAL;
  899. }
  900. port->bulk_out_endpointAddress = bulk_out_endpoint->bEndpointAddress;
  901. port->bulk_in_endpointAddress = bulk_in_endpoint->bEndpointAddress;
  902. portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
  903. if (!portdata) {
  904. dev_dbg(&port->dev, "%s: kmalloc for vizzini_port_private (%d) failed!.\n",
  905. __func__, i);
  906. return -ENOMEM;
  907. }
  908. spin_lock_init(&portdata->lock);
  909. for (j = 0; j < N_IN_URB; j++) {
  910. portdata->in_buffer[j] = kmalloc(IN_BUFLEN, GFP_KERNEL);
  911. if (!portdata->in_buffer[j]) {
  912. for (--j; j >= 0; j--)
  913. kfree(portdata->in_buffer[j]);
  914. kfree(portdata);
  915. return -ENOMEM;
  916. }
  917. }
  918. /* Bulk OUT endpoints 0x1..0x4 map to register blocks 0..3 */
  919. portdata->block = port->bulk_out_endpointAddress - 1;
  920. usb_set_serial_port_data(port, portdata);
  921. portdata->bcd_device = le16_to_cpu(serial->dev->descriptor.bcdDevice);
  922. if (vizzini_rev_a(port))
  923. dev_info(&port->dev, "Adapting to revA silicon\n");
  924. /* initialize the in urbs */
  925. for (j = 0; j < N_IN_URB; ++j) {
  926. urb = usb_alloc_urb(0, GFP_KERNEL);
  927. if (urb == NULL) {
  928. dev_dbg(&port->dev, "%s: alloc for in port failed.\n", __func__);
  929. continue;
  930. }
  931. /* Fill URB using supplied data. */
  932. dev_dbg(&port->dev, "Filling URB %p, EP=%d buf=%p len=%d\n", urb, port->bulk_in_endpointAddress, portdata->in_buffer[j], IN_BUFLEN);
  933. usb_fill_bulk_urb(urb, serial->dev,
  934. usb_rcvbulkpipe(serial->dev,
  935. port->bulk_in_endpointAddress),
  936. portdata->in_buffer[j], IN_BUFLEN,
  937. vizzini_in_callback, port);
  938. portdata->in_urbs[j] = urb;
  939. }
  940. return 0;
  941. }
  942. static void vizzini_serial_disconnect(struct usb_serial *serial)
  943. {
  944. struct usb_serial_port *port;
  945. struct vizzini_port_private *portdata;
  946. int i, j;
  947. dev_dbg(&serial->dev->dev, "%s %p\n", __func__, serial);
  948. for (i = 0; i < serial->num_ports; ++i) {
  949. port = serial->port[i];
  950. if (!port)
  951. continue;
  952. portdata = usb_get_serial_port_data(port);
  953. if (!portdata)
  954. continue;
  955. for (j = 0; j < N_IN_URB; j++) {
  956. usb_kill_urb(portdata->in_urbs[j]);
  957. usb_free_urb(portdata->in_urbs[j]);
  958. }
  959. }
  960. }
  961. static void vizzini_serial_release(struct usb_serial *serial)
  962. {
  963. struct usb_serial_port *port;
  964. struct vizzini_port_private *portdata;
  965. int i, j;
  966. dev_dbg(&serial->dev->dev, "%s %p\n", __func__, serial);
  967. for (i = 0; i < serial->num_ports; ++i) {
  968. port = serial->port[i];
  969. if (!port)
  970. continue;
  971. portdata = usb_get_serial_port_data(port);
  972. if (!portdata)
  973. continue;
  974. for (j = 0; j < N_IN_URB; j++)
  975. kfree(portdata->in_buffer[j]);
  976. kfree(portdata);
  977. usb_set_serial_port_data(port, NULL);
  978. }
  979. }
  980. static int vizzini_calc_num_ports(struct usb_serial *serial)
  981. {
  982. return 1;
  983. }
  984. static int vizzini_probe(struct usb_serial *serial,
  985. const struct usb_device_id *id)
  986. {
  987. struct usb_interface *intf = serial->interface;
  988. unsigned char *buffer = intf->altsetting->extra;
  989. int buflen = intf->altsetting->extralen;
  990. struct usb_device *usb_dev = interface_to_usbdev(intf);
  991. struct usb_cdc_union_desc *union_header = NULL;
  992. struct usb_cdc_country_functional_desc *cfd = NULL;
  993. int call_interface_num = -1;
  994. int data_interface_num;
  995. struct usb_interface *control_interface;
  996. struct usb_interface *data_interface;
  997. struct usb_endpoint_descriptor *epctrl;
  998. struct usb_endpoint_descriptor *epread;
  999. struct usb_endpoint_descriptor *epwrite;
  1000. struct vizzini_serial_private *serial_priv;
  1001. if (!buffer) {
  1002. dev_err(&intf->dev, "Weird descriptor references\n");
  1003. return -EINVAL;
  1004. }
  1005. if (!buflen) {
  1006. if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
  1007. dev_dbg(&intf->dev, "Seeking extra descriptors on endpoint\n");
  1008. buflen = intf->cur_altsetting->endpoint->extralen;
  1009. buffer = intf->cur_altsetting->endpoint->extra;
  1010. } else {
  1011. dev_err(&intf->dev, "Zero length descriptor references\n");
  1012. return -EINVAL;
  1013. }
  1014. }
  1015. while (buflen > 0) {
  1016. if (buffer[1] != USB_DT_CS_INTERFACE) {
  1017. dev_err(&intf->dev, "skipping garbage\n");
  1018. goto next_desc;
  1019. }
  1020. switch (buffer[2]) {
  1021. case USB_CDC_UNION_TYPE: /* we've found it */
  1022. if (union_header) {
  1023. dev_err(&intf->dev, "More than one union descriptor, skipping ...\n");
  1024. goto next_desc;
  1025. }
  1026. union_header = (struct usb_cdc_union_desc *)buffer;
  1027. break;
  1028. case USB_CDC_COUNTRY_TYPE: /* export through sysfs */
  1029. cfd = (struct usb_cdc_country_functional_desc *)buffer;
  1030. break;
  1031. case USB_CDC_HEADER_TYPE: /* maybe check version */
  1032. break; /* for now we ignore it */
  1033. case USB_CDC_CALL_MANAGEMENT_TYPE:
  1034. call_interface_num = buffer[4];
  1035. break;
  1036. default:
  1037. /* there are LOTS more CDC descriptors that
  1038. * could legitimately be found here.
  1039. */
  1040. dev_dbg(&intf->dev, "Ignoring descriptor: type %02x, length %d\n", buffer[2], buffer[0]);
  1041. break;
  1042. }
  1043. next_desc:
  1044. buflen -= buffer[0];
  1045. buffer += buffer[0];
  1046. }
  1047. if (!union_header) {
  1048. if (call_interface_num > 0) {
  1049. dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
  1050. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
  1051. control_interface = intf;
  1052. } else {
  1053. dev_dbg(&intf->dev, "No union descriptor, giving up\n");
  1054. return -ENODEV;
  1055. }
  1056. } else {
  1057. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  1058. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
  1059. if (!control_interface || !data_interface) {
  1060. dev_dbg(&intf->dev, "no interfaces\n");
  1061. return -ENODEV;
  1062. }
  1063. }
  1064. if (data_interface_num != call_interface_num)
  1065. dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
  1066. /* workaround for switched interfaces */
  1067. if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
  1068. if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
  1069. struct usb_interface *t;
  1070. t = control_interface;
  1071. control_interface = data_interface;
  1072. data_interface = t;
  1073. } else {
  1074. return -EINVAL;
  1075. }
  1076. }
  1077. /* Accept probe requests only for the control interface */
  1078. if (intf != control_interface)
  1079. return -ENODEV;
  1080. if (usb_interface_claimed(data_interface)) { /* valid in this context */
  1081. dev_dbg(&intf->dev, "The data interface isn't available\n");
  1082. return -EBUSY;
  1083. }
  1084. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
  1085. return -EINVAL;
  1086. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  1087. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  1088. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  1089. if (!usb_endpoint_dir_in(epread)) {
  1090. struct usb_endpoint_descriptor *t;
  1091. t = epread;
  1092. epread = epwrite;
  1093. epwrite = t;
  1094. }
  1095. /* The documentation suggests that we allocate private storage
  1096. * with the attach() entry point, but we can't allow the data
  1097. * interface to remain unclaimed until then; so we need
  1098. * somewhere to save the claimed interface now. */
  1099. serial_priv = kzalloc(sizeof(struct vizzini_serial_private),
  1100. GFP_KERNEL);
  1101. if (!serial_priv)
  1102. goto alloc_fail;
  1103. usb_set_serial_data(serial, serial_priv);
  1104. //usb_driver_claim_interface(&vizzini_driver, data_interface, NULL);
  1105. /* Don't set the data interface private data. When we
  1106. * disconnect we test this field against NULL to discover
  1107. * whether we're dealing with the control or data
  1108. * interface. */
  1109. serial_priv->data_interface = data_interface;
  1110. return 0;
  1111. alloc_fail:
  1112. return -ENOMEM;
  1113. }
  1114. static struct usb_serial_driver vizzini_device = {
  1115. .driver = {
  1116. .owner = THIS_MODULE,
  1117. .name = "vizzini",
  1118. },
  1119. .description = "Vizzini USB serial port",
  1120. .id_table = id_table,
  1121. .calc_num_ports = vizzini_calc_num_ports,
  1122. .probe = vizzini_probe,
  1123. .open = vizzini_open,
  1124. .close = vizzini_close,
  1125. .write = vizzini_write,
  1126. .write_room = vizzini_write_room,
  1127. .ioctl = vizzini_ioctl,
  1128. .set_termios = vizzini_set_termios,
  1129. .break_ctl = vizzini_break_ctl,
  1130. .tiocmget = vizzini_tiocmget,
  1131. .tiocmset = vizzini_tiocmset,
  1132. .attach = vizzini_attach,
  1133. .disconnect = vizzini_serial_disconnect,
  1134. .release = vizzini_serial_release,
  1135. .read_int_callback = vizzini_int_callback,
  1136. };
  1137. static struct usb_serial_driver * const serial_drivers[] = {
  1138. &vizzini_device, NULL
  1139. };
  1140. module_usb_serial_driver(serial_drivers, id_table);
  1141. MODULE_AUTHOR(DRIVER_AUTHOR);
  1142. MODULE_DESCRIPTION(DRIVER_DESC);
  1143. MODULE_VERSION(DRIVER_VERSION);
  1144. MODULE_LICENSE("GPL");