mos7720.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. /*
  2. * mos7720.c
  3. * Controls the Moschip 7720 usb to dual port serial convertor
  4. *
  5. * Copyright 2006 Moschip Semiconductor Tech. Ltd.
  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. * Developed by:
  12. * Vijaya Kumar <vijaykumar.gn@gmail.com>
  13. * Ajay Kumar <naanuajay@yahoo.com>
  14. * Gurudeva <ngurudeva@yahoo.com>
  15. *
  16. * Cleaned up from the original by:
  17. * Greg Kroah-Hartman <gregkh@suse.de>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/module.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/serial.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <linux/uaccess.h>
  37. /*
  38. * Version Information
  39. */
  40. #define DRIVER_VERSION "1.0.0.4F"
  41. #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  42. #define DRIVER_DESC "Moschip USB Serial Driver"
  43. /* default urb timeout */
  44. #define MOS_WDR_TIMEOUT (HZ * 5)
  45. #define MOS_PORT1 0x0200
  46. #define MOS_PORT2 0x0300
  47. #define MOS_VENREG 0x0000
  48. #define MOS_MAX_PORT 0x02
  49. #define MOS_WRITE 0x0E
  50. #define MOS_READ 0x0D
  51. /* Interrupt Rotinue Defines */
  52. #define SERIAL_IIR_RLS 0x06
  53. #define SERIAL_IIR_RDA 0x04
  54. #define SERIAL_IIR_CTI 0x0c
  55. #define SERIAL_IIR_THR 0x02
  56. #define SERIAL_IIR_MS 0x00
  57. #define NUM_URBS 16 /* URB Count */
  58. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  59. /* This structure holds all of the local port information */
  60. struct moschip_port {
  61. __u8 shadowLCR; /* last LCR value received */
  62. __u8 shadowMCR; /* last MCR value received */
  63. __u8 shadowMSR; /* last MSR value received */
  64. char open;
  65. struct async_icount icount;
  66. struct usb_serial_port *port; /* loop back to the owner */
  67. struct urb *write_urb_pool[NUM_URBS];
  68. };
  69. /* This structure holds all of the individual serial device information */
  70. struct moschip_serial {
  71. int interrupt_started;
  72. };
  73. static int debug;
  74. static struct usb_serial_driver moschip7720_2port_driver;
  75. #define USB_VENDOR_ID_MOSCHIP 0x9710
  76. #define MOSCHIP_DEVICE_ID_7720 0x7720
  77. #define MOSCHIP_DEVICE_ID_7715 0x7715
  78. static const struct usb_device_id moschip_port_id_table[] = {
  79. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
  80. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
  81. { } /* terminating entry */
  82. };
  83. MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
  84. /*
  85. * mos7720_interrupt_callback
  86. * this is the callback function for when we have received data on the
  87. * interrupt endpoint.
  88. */
  89. static void mos7720_interrupt_callback(struct urb *urb)
  90. {
  91. int result;
  92. int length;
  93. int status = urb->status;
  94. __u8 *data;
  95. __u8 sp1;
  96. __u8 sp2;
  97. dbg(" : Entering");
  98. switch (status) {
  99. case 0:
  100. /* success */
  101. break;
  102. case -ECONNRESET:
  103. case -ENOENT:
  104. case -ESHUTDOWN:
  105. /* this urb is terminated, clean up */
  106. dbg("%s - urb shutting down with status: %d", __func__,
  107. status);
  108. return;
  109. default:
  110. dbg("%s - nonzero urb status received: %d", __func__,
  111. status);
  112. goto exit;
  113. }
  114. length = urb->actual_length;
  115. data = urb->transfer_buffer;
  116. /* Moschip get 4 bytes
  117. * Byte 1 IIR Port 1 (port.number is 0)
  118. * Byte 2 IIR Port 2 (port.number is 1)
  119. * Byte 3 --------------
  120. * Byte 4 FIFO status for both */
  121. /* the above description is inverted
  122. * oneukum 2007-03-14 */
  123. if (unlikely(length != 4)) {
  124. dbg("Wrong data !!!");
  125. return;
  126. }
  127. sp1 = data[3];
  128. sp2 = data[2];
  129. if ((sp1 | sp2) & 0x01) {
  130. /* No Interrupt Pending in both the ports */
  131. dbg("No Interrupt !!!");
  132. } else {
  133. switch (sp1 & 0x0f) {
  134. case SERIAL_IIR_RLS:
  135. dbg("Serial Port 1: Receiver status error or address "
  136. "bit detected in 9-bit mode\n");
  137. break;
  138. case SERIAL_IIR_CTI:
  139. dbg("Serial Port 1: Receiver time out");
  140. break;
  141. case SERIAL_IIR_MS:
  142. dbg("Serial Port 1: Modem status change");
  143. break;
  144. }
  145. switch (sp2 & 0x0f) {
  146. case SERIAL_IIR_RLS:
  147. dbg("Serial Port 2: Receiver status error or address "
  148. "bit detected in 9-bit mode");
  149. break;
  150. case SERIAL_IIR_CTI:
  151. dbg("Serial Port 2: Receiver time out");
  152. break;
  153. case SERIAL_IIR_MS:
  154. dbg("Serial Port 2: Modem status change");
  155. break;
  156. }
  157. }
  158. exit:
  159. result = usb_submit_urb(urb, GFP_ATOMIC);
  160. if (result)
  161. dev_err(&urb->dev->dev,
  162. "%s - Error %d submitting control urb\n",
  163. __func__, result);
  164. return;
  165. }
  166. /*
  167. * mos7715_interrupt_callback
  168. * this is the 7715's callback function for when we have received data on
  169. * the interrupt endpoint.
  170. */
  171. static void mos7715_interrupt_callback(struct urb *urb)
  172. {
  173. int result;
  174. int length;
  175. int status = urb->status;
  176. __u8 *data;
  177. __u8 iir;
  178. switch (status) {
  179. case 0:
  180. /* success */
  181. break;
  182. case -ECONNRESET:
  183. case -ENOENT:
  184. case -ESHUTDOWN:
  185. /* this urb is terminated, clean up */
  186. dbg("%s - urb shutting down with status: %d", __func__,
  187. status);
  188. return;
  189. default:
  190. dbg("%s - nonzero urb status received: %d", __func__,
  191. status);
  192. goto exit;
  193. }
  194. length = urb->actual_length;
  195. data = urb->transfer_buffer;
  196. /* Structure of data from 7715 device:
  197. * Byte 1: IIR serial Port
  198. * Byte 2: unused
  199. * Byte 2: DSR parallel port
  200. * Byte 4: FIFO status for both */
  201. if (unlikely(length != 4)) {
  202. dbg("Wrong data !!!");
  203. return;
  204. }
  205. iir = data[0];
  206. if (!(iir & 0x01)) { /* serial port interrupt pending */
  207. switch (iir & 0x0f) {
  208. case SERIAL_IIR_RLS:
  209. dbg("Serial Port: Receiver status error or address "
  210. "bit detected in 9-bit mode\n");
  211. break;
  212. case SERIAL_IIR_CTI:
  213. dbg("Serial Port: Receiver time out");
  214. break;
  215. case SERIAL_IIR_MS:
  216. dbg("Serial Port: Modem status change");
  217. break;
  218. }
  219. }
  220. exit:
  221. result = usb_submit_urb(urb, GFP_ATOMIC);
  222. if (result)
  223. dev_err(&urb->dev->dev,
  224. "%s - Error %d submitting control urb\n",
  225. __func__, result);
  226. return;
  227. }
  228. /*
  229. * mos7720_bulk_in_callback
  230. * this is the callback function for when we have received data on the
  231. * bulk in endpoint.
  232. */
  233. static void mos7720_bulk_in_callback(struct urb *urb)
  234. {
  235. int retval;
  236. unsigned char *data ;
  237. struct usb_serial_port *port;
  238. struct moschip_port *mos7720_port;
  239. struct tty_struct *tty;
  240. int status = urb->status;
  241. if (status) {
  242. dbg("nonzero read bulk status received: %d", status);
  243. return;
  244. }
  245. mos7720_port = urb->context;
  246. if (!mos7720_port) {
  247. dbg("NULL mos7720_port pointer");
  248. return ;
  249. }
  250. port = mos7720_port->port;
  251. dbg("Entering...%s", __func__);
  252. data = urb->transfer_buffer;
  253. tty = tty_port_tty_get(&port->port);
  254. if (tty && urb->actual_length) {
  255. tty_buffer_request_room(tty, urb->actual_length);
  256. tty_insert_flip_string(tty, data, urb->actual_length);
  257. tty_flip_buffer_push(tty);
  258. }
  259. tty_kref_put(tty);
  260. if (!port->read_urb) {
  261. dbg("URB KILLED !!!");
  262. return;
  263. }
  264. if (port->read_urb->status != -EINPROGRESS) {
  265. port->read_urb->dev = port->serial->dev;
  266. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  267. if (retval)
  268. dbg("usb_submit_urb(read bulk) failed, retval = %d",
  269. retval);
  270. }
  271. }
  272. /*
  273. * mos7720_bulk_out_data_callback
  274. * this is the callback function for when we have finished sending serial
  275. * data on the bulk out endpoint.
  276. */
  277. static void mos7720_bulk_out_data_callback(struct urb *urb)
  278. {
  279. struct moschip_port *mos7720_port;
  280. struct tty_struct *tty;
  281. int status = urb->status;
  282. if (status) {
  283. dbg("nonzero write bulk status received:%d", status);
  284. return;
  285. }
  286. mos7720_port = urb->context;
  287. if (!mos7720_port) {
  288. dbg("NULL mos7720_port pointer");
  289. return ;
  290. }
  291. dbg("Entering .........");
  292. tty = tty_port_tty_get(&mos7720_port->port->port);
  293. if (tty && mos7720_port->open)
  294. tty_wakeup(tty);
  295. tty_kref_put(tty);
  296. }
  297. /*
  298. * send_mos_cmd
  299. * this function will be used for sending command to device
  300. */
  301. static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
  302. __u16 index, u8 *data)
  303. {
  304. int status;
  305. u8 *buf;
  306. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  307. if (value < MOS_MAX_PORT) {
  308. if (product == MOSCHIP_DEVICE_ID_7715)
  309. value = 0x0200; /* identifies the 7715's serial port */
  310. else
  311. value = value*0x100+0x200;
  312. } else {
  313. value = 0x0000;
  314. if ((product == MOSCHIP_DEVICE_ID_7715) &&
  315. (index != 0x08)) {
  316. dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
  317. /* index = 0x01 ; */
  318. }
  319. }
  320. if (request == MOS_WRITE) {
  321. value = value + *data;
  322. status = usb_control_msg(serial->dev,
  323. usb_sndctrlpipe(serial->dev, 0), MOS_WRITE,
  324. 0x40, value, index, NULL, 0, MOS_WDR_TIMEOUT);
  325. } else {
  326. buf = kmalloc(1, GFP_KERNEL);
  327. if (!buf) {
  328. status = -ENOMEM;
  329. goto out;
  330. }
  331. status = usb_control_msg(serial->dev,
  332. usb_rcvctrlpipe(serial->dev, 0), MOS_READ,
  333. 0xc0, value, index, buf, 1, MOS_WDR_TIMEOUT);
  334. *data = *buf;
  335. kfree(buf);
  336. }
  337. out:
  338. if (status < 0)
  339. dbg("Command Write failed Value %x index %x", value, index);
  340. return status;
  341. }
  342. /*
  343. * mos77xx_probe
  344. * this function installs the appropriate read interrupt endpoint callback
  345. * depending on whether the device is a 7720 or 7715, thus avoiding costly
  346. * run-time checks in the high-frequency callback routine itself.
  347. */
  348. static int mos77xx_probe(struct usb_serial *serial,
  349. const struct usb_device_id *id)
  350. {
  351. if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
  352. moschip7720_2port_driver.read_int_callback =
  353. mos7715_interrupt_callback;
  354. else
  355. moschip7720_2port_driver.read_int_callback =
  356. mos7720_interrupt_callback;
  357. return 0;
  358. }
  359. static int mos77xx_calc_num_ports(struct usb_serial *serial)
  360. {
  361. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  362. if (product == MOSCHIP_DEVICE_ID_7715)
  363. return 1;
  364. return 2;
  365. }
  366. static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
  367. {
  368. struct usb_serial *serial;
  369. struct usb_serial_port *port0;
  370. struct urb *urb;
  371. struct moschip_serial *mos7720_serial;
  372. struct moschip_port *mos7720_port;
  373. int response;
  374. int port_number;
  375. char data;
  376. int allocated_urbs = 0;
  377. int j;
  378. serial = port->serial;
  379. mos7720_port = usb_get_serial_port_data(port);
  380. if (mos7720_port == NULL)
  381. return -ENODEV;
  382. port0 = serial->port[0];
  383. mos7720_serial = usb_get_serial_data(serial);
  384. if (mos7720_serial == NULL || port0 == NULL)
  385. return -ENODEV;
  386. usb_clear_halt(serial->dev, port->write_urb->pipe);
  387. usb_clear_halt(serial->dev, port->read_urb->pipe);
  388. /* Initialising the write urb pool */
  389. for (j = 0; j < NUM_URBS; ++j) {
  390. urb = usb_alloc_urb(0, GFP_KERNEL);
  391. mos7720_port->write_urb_pool[j] = urb;
  392. if (urb == NULL) {
  393. dev_err(&port->dev, "No more urbs???\n");
  394. continue;
  395. }
  396. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  397. GFP_KERNEL);
  398. if (!urb->transfer_buffer) {
  399. dev_err(&port->dev,
  400. "%s-out of memory for urb buffers.\n",
  401. __func__);
  402. usb_free_urb(mos7720_port->write_urb_pool[j]);
  403. mos7720_port->write_urb_pool[j] = NULL;
  404. continue;
  405. }
  406. allocated_urbs++;
  407. }
  408. if (!allocated_urbs)
  409. return -ENOMEM;
  410. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  411. *
  412. * Register Index
  413. * 0 : THR/RHR
  414. * 1 : IER
  415. * 2 : FCR
  416. * 3 : LCR
  417. * 4 : MCR
  418. * 5 : LSR
  419. * 6 : MSR
  420. * 7 : SPR
  421. *
  422. * 0x08 : SP1/2 Control Reg
  423. */
  424. port_number = port->number - port->serial->minor;
  425. send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
  426. dbg("SS::%p LSR:%x", mos7720_port, data);
  427. dbg("Check:Sending Command ..........");
  428. data = 0x02;
  429. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
  430. data = 0x02;
  431. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
  432. data = 0x00;
  433. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  434. data = 0x00;
  435. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  436. data = 0xCF;
  437. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  438. data = 0x03;
  439. mos7720_port->shadowLCR = data;
  440. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  441. data = 0x0b;
  442. mos7720_port->shadowMCR = data;
  443. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  444. data = 0x0b;
  445. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  446. data = 0x00;
  447. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  448. data = 0x00;
  449. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  450. /* data = 0x00;
  451. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
  452. data = 0x03;
  453. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  454. data = 0x00;
  455. send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
  456. port_number + 1, &data);
  457. */
  458. data = 0x00;
  459. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  460. data = data | (port->number - port->serial->minor + 1);
  461. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  462. data = 0x83;
  463. mos7720_port->shadowLCR = data;
  464. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  465. data = 0x0c;
  466. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  467. data = 0x00;
  468. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  469. data = 0x03;
  470. mos7720_port->shadowLCR = data;
  471. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  472. data = 0x0c;
  473. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  474. data = 0x0c;
  475. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  476. /* see if we've set up our endpoint info yet *
  477. * (can't set it up in mos7720_startup as the *
  478. * structures were not set up at that time.) */
  479. if (!mos7720_serial->interrupt_started) {
  480. dbg("Interrupt buffer NULL !!!");
  481. /* not set up yet, so do it now */
  482. mos7720_serial->interrupt_started = 1;
  483. dbg("To Submit URB !!!");
  484. /* set up our interrupt urb */
  485. usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
  486. usb_rcvintpipe(serial->dev,
  487. port->interrupt_in_endpointAddress),
  488. port0->interrupt_in_buffer,
  489. port0->interrupt_in_urb->transfer_buffer_length,
  490. mos7720_interrupt_callback, mos7720_port,
  491. port0->interrupt_in_urb->interval);
  492. /* start interrupt read for this mos7720 this interrupt *
  493. * will continue as long as the mos7720 is connected */
  494. dbg("Submit URB over !!!");
  495. response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
  496. if (response)
  497. dev_err(&port->dev,
  498. "%s - Error %d submitting control urb\n",
  499. __func__, response);
  500. }
  501. /* set up our bulk in urb */
  502. usb_fill_bulk_urb(port->read_urb, serial->dev,
  503. usb_rcvbulkpipe(serial->dev,
  504. port->bulk_in_endpointAddress),
  505. port->bulk_in_buffer,
  506. port->read_urb->transfer_buffer_length,
  507. mos7720_bulk_in_callback, mos7720_port);
  508. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  509. if (response)
  510. dev_err(&port->dev, "%s - Error %d submitting read urb\n",
  511. __func__, response);
  512. /* initialize our icount structure */
  513. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  514. /* initialize our port settings */
  515. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  516. /* send a open port command */
  517. mos7720_port->open = 1;
  518. return 0;
  519. }
  520. /*
  521. * mos7720_chars_in_buffer
  522. * this function is called by the tty driver when it wants to know how many
  523. * bytes of data we currently have outstanding in the port (data that has
  524. * been written, but hasn't made it out the port yet)
  525. * If successful, we return the number of bytes left to be written in the
  526. * system,
  527. * Otherwise we return a negative error number.
  528. */
  529. static int mos7720_chars_in_buffer(struct tty_struct *tty)
  530. {
  531. struct usb_serial_port *port = tty->driver_data;
  532. int i;
  533. int chars = 0;
  534. struct moschip_port *mos7720_port;
  535. dbg("%s:entering ...........", __func__);
  536. mos7720_port = usb_get_serial_port_data(port);
  537. if (mos7720_port == NULL) {
  538. dbg("%s:leaving ...........", __func__);
  539. return 0;
  540. }
  541. for (i = 0; i < NUM_URBS; ++i) {
  542. if (mos7720_port->write_urb_pool[i] &&
  543. mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  544. chars += URB_TRANSFER_BUFFER_SIZE;
  545. }
  546. dbg("%s - returns %d", __func__, chars);
  547. return chars;
  548. }
  549. static void mos7720_close(struct usb_serial_port *port)
  550. {
  551. struct usb_serial *serial;
  552. struct moschip_port *mos7720_port;
  553. char data;
  554. int j;
  555. dbg("mos7720_close:entering...");
  556. serial = port->serial;
  557. mos7720_port = usb_get_serial_port_data(port);
  558. if (mos7720_port == NULL)
  559. return;
  560. for (j = 0; j < NUM_URBS; ++j)
  561. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  562. /* Freeing Write URBs */
  563. for (j = 0; j < NUM_URBS; ++j) {
  564. if (mos7720_port->write_urb_pool[j]) {
  565. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  566. usb_free_urb(mos7720_port->write_urb_pool[j]);
  567. }
  568. }
  569. /* While closing port, shutdown all bulk read, write *
  570. * and interrupt read if they exists, otherwise nop */
  571. dbg("Shutdown bulk write");
  572. usb_kill_urb(port->write_urb);
  573. dbg("Shutdown bulk read");
  574. usb_kill_urb(port->read_urb);
  575. mutex_lock(&serial->disc_mutex);
  576. /* these commands must not be issued if the device has
  577. * been disconnected */
  578. if (!serial->disconnected) {
  579. data = 0x00;
  580. send_mos_cmd(serial, MOS_WRITE,
  581. port->number - port->serial->minor, 0x04, &data);
  582. data = 0x00;
  583. send_mos_cmd(serial, MOS_WRITE,
  584. port->number - port->serial->minor, 0x01, &data);
  585. }
  586. mutex_unlock(&serial->disc_mutex);
  587. mos7720_port->open = 0;
  588. dbg("Leaving %s", __func__);
  589. }
  590. static void mos7720_break(struct tty_struct *tty, int break_state)
  591. {
  592. struct usb_serial_port *port = tty->driver_data;
  593. unsigned char data;
  594. struct usb_serial *serial;
  595. struct moschip_port *mos7720_port;
  596. dbg("Entering %s", __func__);
  597. serial = port->serial;
  598. mos7720_port = usb_get_serial_port_data(port);
  599. if (mos7720_port == NULL)
  600. return;
  601. if (break_state == -1)
  602. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  603. else
  604. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  605. mos7720_port->shadowLCR = data;
  606. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  607. 0x03, &data);
  608. return;
  609. }
  610. /*
  611. * mos7720_write_room
  612. * this function is called by the tty driver when it wants to know how many
  613. * bytes of data we can accept for a specific port.
  614. * If successful, we return the amount of room that we have for this port
  615. * Otherwise we return a negative error number.
  616. */
  617. static int mos7720_write_room(struct tty_struct *tty)
  618. {
  619. struct usb_serial_port *port = tty->driver_data;
  620. struct moschip_port *mos7720_port;
  621. int room = 0;
  622. int i;
  623. dbg("%s:entering ...........", __func__);
  624. mos7720_port = usb_get_serial_port_data(port);
  625. if (mos7720_port == NULL) {
  626. dbg("%s:leaving ...........", __func__);
  627. return -ENODEV;
  628. }
  629. /* FIXME: Locking */
  630. for (i = 0; i < NUM_URBS; ++i) {
  631. if (mos7720_port->write_urb_pool[i] &&
  632. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  633. room += URB_TRANSFER_BUFFER_SIZE;
  634. }
  635. dbg("%s - returns %d", __func__, room);
  636. return room;
  637. }
  638. static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
  639. const unsigned char *data, int count)
  640. {
  641. int status;
  642. int i;
  643. int bytes_sent = 0;
  644. int transfer_size;
  645. struct moschip_port *mos7720_port;
  646. struct usb_serial *serial;
  647. struct urb *urb;
  648. const unsigned char *current_position = data;
  649. dbg("%s:entering ...........", __func__);
  650. serial = port->serial;
  651. mos7720_port = usb_get_serial_port_data(port);
  652. if (mos7720_port == NULL) {
  653. dbg("mos7720_port is NULL");
  654. return -ENODEV;
  655. }
  656. /* try to find a free urb in the list */
  657. urb = NULL;
  658. for (i = 0; i < NUM_URBS; ++i) {
  659. if (mos7720_port->write_urb_pool[i] &&
  660. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  661. urb = mos7720_port->write_urb_pool[i];
  662. dbg("URB:%d", i);
  663. break;
  664. }
  665. }
  666. if (urb == NULL) {
  667. dbg("%s - no more free urbs", __func__);
  668. goto exit;
  669. }
  670. if (urb->transfer_buffer == NULL) {
  671. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  672. GFP_KERNEL);
  673. if (urb->transfer_buffer == NULL) {
  674. dev_err(&port->dev, "%s no more kernel memory...\n",
  675. __func__);
  676. goto exit;
  677. }
  678. }
  679. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  680. memcpy(urb->transfer_buffer, current_position, transfer_size);
  681. usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
  682. urb->transfer_buffer);
  683. /* fill urb with data and submit */
  684. usb_fill_bulk_urb(urb, serial->dev,
  685. usb_sndbulkpipe(serial->dev,
  686. port->bulk_out_endpointAddress),
  687. urb->transfer_buffer, transfer_size,
  688. mos7720_bulk_out_data_callback, mos7720_port);
  689. /* send it down the pipe */
  690. status = usb_submit_urb(urb, GFP_ATOMIC);
  691. if (status) {
  692. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
  693. "with status = %d\n", __func__, status);
  694. bytes_sent = status;
  695. goto exit;
  696. }
  697. bytes_sent = transfer_size;
  698. exit:
  699. return bytes_sent;
  700. }
  701. static void mos7720_throttle(struct tty_struct *tty)
  702. {
  703. struct usb_serial_port *port = tty->driver_data;
  704. struct moschip_port *mos7720_port;
  705. int status;
  706. dbg("%s- port %d", __func__, port->number);
  707. mos7720_port = usb_get_serial_port_data(port);
  708. if (mos7720_port == NULL)
  709. return;
  710. if (!mos7720_port->open) {
  711. dbg("port not opened");
  712. return;
  713. }
  714. dbg("%s: Entering ..........", __func__);
  715. /* if we are implementing XON/XOFF, send the stop character */
  716. if (I_IXOFF(tty)) {
  717. unsigned char stop_char = STOP_CHAR(tty);
  718. status = mos7720_write(tty, port, &stop_char, 1);
  719. if (status <= 0)
  720. return;
  721. }
  722. /* if we are implementing RTS/CTS, toggle that line */
  723. if (tty->termios->c_cflag & CRTSCTS) {
  724. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  725. status = send_mos_cmd(port->serial, MOS_WRITE,
  726. port->number - port->serial->minor,
  727. UART_MCR, &mos7720_port->shadowMCR);
  728. if (status != 0)
  729. return;
  730. }
  731. }
  732. static void mos7720_unthrottle(struct tty_struct *tty)
  733. {
  734. struct usb_serial_port *port = tty->driver_data;
  735. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  736. int status;
  737. if (mos7720_port == NULL)
  738. return;
  739. if (!mos7720_port->open) {
  740. dbg("%s - port not opened", __func__);
  741. return;
  742. }
  743. dbg("%s: Entering ..........", __func__);
  744. /* if we are implementing XON/XOFF, send the start character */
  745. if (I_IXOFF(tty)) {
  746. unsigned char start_char = START_CHAR(tty);
  747. status = mos7720_write(tty, port, &start_char, 1);
  748. if (status <= 0)
  749. return;
  750. }
  751. /* if we are implementing RTS/CTS, toggle that line */
  752. if (tty->termios->c_cflag & CRTSCTS) {
  753. mos7720_port->shadowMCR |= UART_MCR_RTS;
  754. status = send_mos_cmd(port->serial, MOS_WRITE,
  755. port->number - port->serial->minor,
  756. UART_MCR, &mos7720_port->shadowMCR);
  757. if (status != 0)
  758. return;
  759. }
  760. }
  761. static int set_higher_rates(struct moschip_port *mos7720_port,
  762. unsigned int baud)
  763. {
  764. unsigned char data;
  765. struct usb_serial_port *port;
  766. struct usb_serial *serial;
  767. int port_number;
  768. if (mos7720_port == NULL)
  769. return -EINVAL;
  770. port = mos7720_port->port;
  771. serial = port->serial;
  772. /***********************************************
  773. * Init Sequence for higher rates
  774. ***********************************************/
  775. dbg("Sending Setting Commands ..........");
  776. port_number = port->number - port->serial->minor;
  777. data = 0x000;
  778. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  779. data = 0x000;
  780. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  781. data = 0x0CF;
  782. send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
  783. data = 0x00b;
  784. mos7720_port->shadowMCR = data;
  785. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  786. data = 0x00b;
  787. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  788. data = 0x000;
  789. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  790. data = 0x000;
  791. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  792. /***********************************************
  793. * Set for higher rates *
  794. ***********************************************/
  795. data = baud * 0x10;
  796. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  797. data = 0x003;
  798. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  799. data = 0x003;
  800. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  801. data = 0x02b;
  802. mos7720_port->shadowMCR = data;
  803. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  804. data = 0x02b;
  805. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  806. /***********************************************
  807. * Set DLL/DLM
  808. ***********************************************/
  809. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  810. mos7720_port->shadowLCR = data;
  811. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  812. data = 0x001; /* DLL */
  813. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  814. data = 0x000; /* DLM */
  815. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  816. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  817. mos7720_port->shadowLCR = data;
  818. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  819. return 0;
  820. }
  821. /* baud rate information */
  822. struct divisor_table_entry {
  823. __u32 baudrate;
  824. __u16 divisor;
  825. };
  826. /* Define table of divisors for moschip 7720 hardware *
  827. * These assume a 3.6864MHz crystal, the standard /16, and *
  828. * MCR.7 = 0. */
  829. static struct divisor_table_entry divisor_table[] = {
  830. { 50, 2304},
  831. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  832. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  833. { 150, 768},
  834. { 300, 384},
  835. { 600, 192},
  836. { 1200, 96},
  837. { 1800, 64},
  838. { 2400, 48},
  839. { 4800, 24},
  840. { 7200, 16},
  841. { 9600, 12},
  842. { 19200, 6},
  843. { 38400, 3},
  844. { 57600, 2},
  845. { 115200, 1},
  846. };
  847. /*****************************************************************************
  848. * calc_baud_rate_divisor
  849. * this function calculates the proper baud rate divisor for the specified
  850. * baud rate.
  851. *****************************************************************************/
  852. static int calc_baud_rate_divisor(int baudrate, int *divisor)
  853. {
  854. int i;
  855. __u16 custom;
  856. __u16 round1;
  857. __u16 round;
  858. dbg("%s - %d", __func__, baudrate);
  859. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  860. if (divisor_table[i].baudrate == baudrate) {
  861. *divisor = divisor_table[i].divisor;
  862. return 0;
  863. }
  864. }
  865. /* After trying for all the standard baud rates *
  866. * Try calculating the divisor for this baud rate */
  867. if (baudrate > 75 && baudrate < 230400) {
  868. /* get the divisor */
  869. custom = (__u16)(230400L / baudrate);
  870. /* Check for round off */
  871. round1 = (__u16)(2304000L / baudrate);
  872. round = (__u16)(round1 - (custom * 10));
  873. if (round > 4)
  874. custom++;
  875. *divisor = custom;
  876. dbg("Baud %d = %d", baudrate, custom);
  877. return 0;
  878. }
  879. dbg("Baud calculation Failed...");
  880. return -EINVAL;
  881. }
  882. /*
  883. * send_cmd_write_baud_rate
  884. * this function sends the proper command to change the baud rate of the
  885. * specified port.
  886. */
  887. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  888. int baudrate)
  889. {
  890. struct usb_serial_port *port;
  891. struct usb_serial *serial;
  892. int divisor;
  893. int status;
  894. unsigned char data;
  895. unsigned char number;
  896. if (mos7720_port == NULL)
  897. return -1;
  898. port = mos7720_port->port;
  899. serial = port->serial;
  900. dbg("%s: Entering ..........", __func__);
  901. number = port->number - port->serial->minor;
  902. dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
  903. /* Calculate the Divisor */
  904. status = calc_baud_rate_divisor(baudrate, &divisor);
  905. if (status) {
  906. dev_err(&port->dev, "%s - bad baud rate\n", __func__);
  907. return status;
  908. }
  909. /* Enable access to divisor latch */
  910. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  911. mos7720_port->shadowLCR = data;
  912. send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
  913. /* Write the divisor */
  914. data = ((unsigned char)(divisor & 0xff));
  915. send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
  916. data = ((unsigned char)((divisor & 0xff00) >> 8));
  917. send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
  918. /* Disable access to divisor latch */
  919. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  920. mos7720_port->shadowLCR = data;
  921. send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
  922. return status;
  923. }
  924. /*
  925. * change_port_settings
  926. * This routine is called to set the UART on the device to match
  927. * the specified new settings.
  928. */
  929. static void change_port_settings(struct tty_struct *tty,
  930. struct moschip_port *mos7720_port,
  931. struct ktermios *old_termios)
  932. {
  933. struct usb_serial_port *port;
  934. struct usb_serial *serial;
  935. int baud;
  936. unsigned cflag;
  937. unsigned iflag;
  938. __u8 mask = 0xff;
  939. __u8 lData;
  940. __u8 lParity;
  941. __u8 lStop;
  942. int status;
  943. int port_number;
  944. char data;
  945. if (mos7720_port == NULL)
  946. return ;
  947. port = mos7720_port->port;
  948. serial = port->serial;
  949. port_number = port->number - port->serial->minor;
  950. dbg("%s - port %d", __func__, port->number);
  951. if (!mos7720_port->open) {
  952. dbg("%s - port not opened", __func__);
  953. return;
  954. }
  955. dbg("%s: Entering ..........", __func__);
  956. lData = UART_LCR_WLEN8;
  957. lStop = 0x00; /* 1 stop bit */
  958. lParity = 0x00; /* No parity */
  959. cflag = tty->termios->c_cflag;
  960. iflag = tty->termios->c_iflag;
  961. /* Change the number of bits */
  962. switch (cflag & CSIZE) {
  963. case CS5:
  964. lData = UART_LCR_WLEN5;
  965. mask = 0x1f;
  966. break;
  967. case CS6:
  968. lData = UART_LCR_WLEN6;
  969. mask = 0x3f;
  970. break;
  971. case CS7:
  972. lData = UART_LCR_WLEN7;
  973. mask = 0x7f;
  974. break;
  975. default:
  976. case CS8:
  977. lData = UART_LCR_WLEN8;
  978. break;
  979. }
  980. /* Change the Parity bit */
  981. if (cflag & PARENB) {
  982. if (cflag & PARODD) {
  983. lParity = UART_LCR_PARITY;
  984. dbg("%s - parity = odd", __func__);
  985. } else {
  986. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  987. dbg("%s - parity = even", __func__);
  988. }
  989. } else {
  990. dbg("%s - parity = none", __func__);
  991. }
  992. if (cflag & CMSPAR)
  993. lParity = lParity | 0x20;
  994. /* Change the Stop bit */
  995. if (cflag & CSTOPB) {
  996. lStop = UART_LCR_STOP;
  997. dbg("%s - stop bits = 2", __func__);
  998. } else {
  999. lStop = 0x00;
  1000. dbg("%s - stop bits = 1", __func__);
  1001. }
  1002. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  1003. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  1004. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  1005. /* Update the LCR with the correct value */
  1006. mos7720_port->shadowLCR &=
  1007. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1008. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  1009. /* Disable Interrupts */
  1010. data = 0x00;
  1011. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  1012. UART_IER, &data);
  1013. data = 0x00;
  1014. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  1015. data = 0xcf;
  1016. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  1017. /* Send the updated LCR value to the mos7720 */
  1018. data = mos7720_port->shadowLCR;
  1019. send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
  1020. data = 0x00b;
  1021. mos7720_port->shadowMCR = data;
  1022. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  1023. data = 0x00b;
  1024. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  1025. /* set up the MCR register and send it to the mos7720 */
  1026. mos7720_port->shadowMCR = UART_MCR_OUT2;
  1027. if (cflag & CBAUD)
  1028. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  1029. if (cflag & CRTSCTS) {
  1030. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  1031. /* To set hardware flow control to the specified *
  1032. * serial port, in SP1/2_CONTROL_REG */
  1033. if (port->number) {
  1034. data = 0x001;
  1035. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  1036. 0x08, &data);
  1037. } else {
  1038. data = 0x002;
  1039. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  1040. 0x08, &data);
  1041. }
  1042. } else {
  1043. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  1044. }
  1045. data = mos7720_port->shadowMCR;
  1046. send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
  1047. /* Determine divisor based on baud rate */
  1048. baud = tty_get_baud_rate(tty);
  1049. if (!baud) {
  1050. /* pick a default, any default... */
  1051. dbg("Picked default baud...");
  1052. baud = 9600;
  1053. }
  1054. if (baud >= 230400) {
  1055. set_higher_rates(mos7720_port, baud);
  1056. /* Enable Interrupts */
  1057. data = 0x0c;
  1058. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  1059. return;
  1060. }
  1061. dbg("%s - baud rate = %d", __func__, baud);
  1062. status = send_cmd_write_baud_rate(mos7720_port, baud);
  1063. /* FIXME: needs to write actual resulting baud back not just
  1064. blindly do so */
  1065. if (cflag & CBAUD)
  1066. tty_encode_baud_rate(tty, baud, baud);
  1067. /* Enable Interrupts */
  1068. data = 0x0c;
  1069. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  1070. if (port->read_urb->status != -EINPROGRESS) {
  1071. port->read_urb->dev = serial->dev;
  1072. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1073. if (status)
  1074. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1075. status);
  1076. }
  1077. return;
  1078. }
  1079. /*
  1080. * mos7720_set_termios
  1081. * this function is called by the tty driver when it wants to change the
  1082. * termios structure.
  1083. */
  1084. static void mos7720_set_termios(struct tty_struct *tty,
  1085. struct usb_serial_port *port, struct ktermios *old_termios)
  1086. {
  1087. int status;
  1088. unsigned int cflag;
  1089. struct usb_serial *serial;
  1090. struct moschip_port *mos7720_port;
  1091. serial = port->serial;
  1092. mos7720_port = usb_get_serial_port_data(port);
  1093. if (mos7720_port == NULL)
  1094. return;
  1095. if (!mos7720_port->open) {
  1096. dbg("%s - port not opened", __func__);
  1097. return;
  1098. }
  1099. dbg("setting termios - ASPIRE");
  1100. cflag = tty->termios->c_cflag;
  1101. dbg("%s - cflag %08x iflag %08x", __func__,
  1102. tty->termios->c_cflag,
  1103. RELEVANT_IFLAG(tty->termios->c_iflag));
  1104. dbg("%s - old cflag %08x old iflag %08x", __func__,
  1105. old_termios->c_cflag,
  1106. RELEVANT_IFLAG(old_termios->c_iflag));
  1107. dbg("%s - port %d", __func__, port->number);
  1108. /* change the port settings to the new ones specified */
  1109. change_port_settings(tty, mos7720_port, old_termios);
  1110. if (!port->read_urb) {
  1111. dbg("URB KILLED !!!!!");
  1112. return;
  1113. }
  1114. if (port->read_urb->status != -EINPROGRESS) {
  1115. port->read_urb->dev = serial->dev;
  1116. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1117. if (status)
  1118. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1119. status);
  1120. }
  1121. return;
  1122. }
  1123. /*
  1124. * get_lsr_info - get line status register info
  1125. *
  1126. * Purpose: Let user call ioctl() to get info when the UART physically
  1127. * is emptied. On bus types like RS485, the transmitter must
  1128. * release the bus after transmitting. This must be done when
  1129. * the transmit shift register is empty, not be done when the
  1130. * transmit holding register is empty. This functionality
  1131. * allows an RS485 driver to be written in user space.
  1132. */
  1133. static int get_lsr_info(struct tty_struct *tty,
  1134. struct moschip_port *mos7720_port, unsigned int __user *value)
  1135. {
  1136. struct usb_serial_port *port = tty->driver_data;
  1137. unsigned int result = 0;
  1138. unsigned char data = 0;
  1139. int port_number = port->number - port->serial->minor;
  1140. int count;
  1141. count = mos7720_chars_in_buffer(tty);
  1142. if (count == 0) {
  1143. send_mos_cmd(port->serial, MOS_READ, port_number,
  1144. UART_LSR, &data);
  1145. if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
  1146. == (UART_LSR_TEMT | UART_LSR_THRE)) {
  1147. dbg("%s -- Empty", __func__);
  1148. result = TIOCSER_TEMT;
  1149. }
  1150. }
  1151. if (copy_to_user(value, &result, sizeof(int)))
  1152. return -EFAULT;
  1153. return 0;
  1154. }
  1155. static int mos7720_tiocmget(struct tty_struct *tty, struct file *file)
  1156. {
  1157. struct usb_serial_port *port = tty->driver_data;
  1158. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1159. unsigned int result = 0;
  1160. unsigned int mcr ;
  1161. unsigned int msr ;
  1162. dbg("%s - port %d", __func__, port->number);
  1163. mcr = mos7720_port->shadowMCR;
  1164. msr = mos7720_port->shadowMSR;
  1165. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
  1166. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
  1167. | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
  1168. | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
  1169. | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
  1170. | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
  1171. dbg("%s -- %x", __func__, result);
  1172. return result;
  1173. }
  1174. static int mos7720_tiocmset(struct tty_struct *tty, struct file *file,
  1175. unsigned int set, unsigned int clear)
  1176. {
  1177. struct usb_serial_port *port = tty->driver_data;
  1178. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1179. unsigned int mcr ;
  1180. unsigned char lmcr;
  1181. dbg("%s - port %d", __func__, port->number);
  1182. dbg("he was at tiocmget");
  1183. mcr = mos7720_port->shadowMCR;
  1184. if (set & TIOCM_RTS)
  1185. mcr |= UART_MCR_RTS;
  1186. if (set & TIOCM_DTR)
  1187. mcr |= UART_MCR_DTR;
  1188. if (set & TIOCM_LOOP)
  1189. mcr |= UART_MCR_LOOP;
  1190. if (clear & TIOCM_RTS)
  1191. mcr &= ~UART_MCR_RTS;
  1192. if (clear & TIOCM_DTR)
  1193. mcr &= ~UART_MCR_DTR;
  1194. if (clear & TIOCM_LOOP)
  1195. mcr &= ~UART_MCR_LOOP;
  1196. mos7720_port->shadowMCR = mcr;
  1197. lmcr = mos7720_port->shadowMCR;
  1198. send_mos_cmd(port->serial, MOS_WRITE,
  1199. port->number - port->serial->minor, UART_MCR, &lmcr);
  1200. return 0;
  1201. }
  1202. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1203. unsigned int __user *value)
  1204. {
  1205. unsigned int mcr ;
  1206. unsigned int arg;
  1207. unsigned char data;
  1208. struct usb_serial_port *port;
  1209. if (mos7720_port == NULL)
  1210. return -1;
  1211. port = (struct usb_serial_port *)mos7720_port->port;
  1212. mcr = mos7720_port->shadowMCR;
  1213. if (copy_from_user(&arg, value, sizeof(int)))
  1214. return -EFAULT;
  1215. switch (cmd) {
  1216. case TIOCMBIS:
  1217. if (arg & TIOCM_RTS)
  1218. mcr |= UART_MCR_RTS;
  1219. if (arg & TIOCM_DTR)
  1220. mcr |= UART_MCR_RTS;
  1221. if (arg & TIOCM_LOOP)
  1222. mcr |= UART_MCR_LOOP;
  1223. break;
  1224. case TIOCMBIC:
  1225. if (arg & TIOCM_RTS)
  1226. mcr &= ~UART_MCR_RTS;
  1227. if (arg & TIOCM_DTR)
  1228. mcr &= ~UART_MCR_RTS;
  1229. if (arg & TIOCM_LOOP)
  1230. mcr &= ~UART_MCR_LOOP;
  1231. break;
  1232. }
  1233. mos7720_port->shadowMCR = mcr;
  1234. data = mos7720_port->shadowMCR;
  1235. send_mos_cmd(port->serial, MOS_WRITE,
  1236. port->number - port->serial->minor, UART_MCR, &data);
  1237. return 0;
  1238. }
  1239. static int get_serial_info(struct moschip_port *mos7720_port,
  1240. struct serial_struct __user *retinfo)
  1241. {
  1242. struct serial_struct tmp;
  1243. if (!retinfo)
  1244. return -EFAULT;
  1245. memset(&tmp, 0, sizeof(tmp));
  1246. tmp.type = PORT_16550A;
  1247. tmp.line = mos7720_port->port->serial->minor;
  1248. tmp.port = mos7720_port->port->number;
  1249. tmp.irq = 0;
  1250. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1251. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1252. tmp.baud_base = 9600;
  1253. tmp.close_delay = 5*HZ;
  1254. tmp.closing_wait = 30*HZ;
  1255. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1256. return -EFAULT;
  1257. return 0;
  1258. }
  1259. static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
  1260. unsigned int cmd, unsigned long arg)
  1261. {
  1262. struct usb_serial_port *port = tty->driver_data;
  1263. struct moschip_port *mos7720_port;
  1264. struct async_icount cnow;
  1265. struct async_icount cprev;
  1266. struct serial_icounter_struct icount;
  1267. mos7720_port = usb_get_serial_port_data(port);
  1268. if (mos7720_port == NULL)
  1269. return -ENODEV;
  1270. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1271. switch (cmd) {
  1272. case TIOCSERGETLSR:
  1273. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1274. return get_lsr_info(tty, mos7720_port,
  1275. (unsigned int __user *)arg);
  1276. return 0;
  1277. /* FIXME: These should be using the mode methods */
  1278. case TIOCMBIS:
  1279. case TIOCMBIC:
  1280. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
  1281. __func__, port->number);
  1282. return set_modem_info(mos7720_port, cmd,
  1283. (unsigned int __user *)arg);
  1284. case TIOCGSERIAL:
  1285. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1286. return get_serial_info(mos7720_port,
  1287. (struct serial_struct __user *)arg);
  1288. case TIOCMIWAIT:
  1289. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1290. cprev = mos7720_port->icount;
  1291. while (1) {
  1292. if (signal_pending(current))
  1293. return -ERESTARTSYS;
  1294. cnow = mos7720_port->icount;
  1295. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1296. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1297. return -EIO; /* no change => error */
  1298. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1299. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1300. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1301. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1302. return 0;
  1303. }
  1304. cprev = cnow;
  1305. }
  1306. /* NOTREACHED */
  1307. break;
  1308. case TIOCGICOUNT:
  1309. cnow = mos7720_port->icount;
  1310. icount.cts = cnow.cts;
  1311. icount.dsr = cnow.dsr;
  1312. icount.rng = cnow.rng;
  1313. icount.dcd = cnow.dcd;
  1314. icount.rx = cnow.rx;
  1315. icount.tx = cnow.tx;
  1316. icount.frame = cnow.frame;
  1317. icount.overrun = cnow.overrun;
  1318. icount.parity = cnow.parity;
  1319. icount.brk = cnow.brk;
  1320. icount.buf_overrun = cnow.buf_overrun;
  1321. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
  1322. port->number, icount.rx, icount.tx);
  1323. if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
  1324. return -EFAULT;
  1325. return 0;
  1326. }
  1327. return -ENOIOCTLCMD;
  1328. }
  1329. static int mos7720_startup(struct usb_serial *serial)
  1330. {
  1331. struct moschip_serial *mos7720_serial;
  1332. struct moschip_port *mos7720_port;
  1333. struct usb_device *dev;
  1334. int i;
  1335. char data;
  1336. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  1337. dbg("%s: Entering ..........", __func__);
  1338. if (!serial) {
  1339. dbg("Invalid Handler");
  1340. return -ENODEV;
  1341. }
  1342. dev = serial->dev;
  1343. /* create our private serial structure */
  1344. mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
  1345. if (mos7720_serial == NULL) {
  1346. dev_err(&dev->dev, "%s - Out of memory\n", __func__);
  1347. return -ENOMEM;
  1348. }
  1349. usb_set_serial_data(serial, mos7720_serial);
  1350. /*
  1351. * The 7715 uses the first bulk in/out endpoint pair for the parallel
  1352. * port, and the second for the serial port. Because the usbserial core
  1353. * assumes both pairs are serial ports, we must engage in a bit of
  1354. * subterfuge and swap the pointers for ports 0 and 1 in order to make
  1355. * port 0 point to the serial port. However, both moschip devices use a
  1356. * single interrupt-in endpoint for both ports (as mentioned a little
  1357. * further down), and this endpoint was assigned to port 0. So after
  1358. * the swap, we must copy the interrupt endpoint elements from port 1
  1359. * (as newly assigned) to port 0, and null out port 1 pointers.
  1360. */
  1361. if (product == MOSCHIP_DEVICE_ID_7715) {
  1362. struct usb_serial_port *tmp = serial->port[0];
  1363. serial->port[0] = serial->port[1];
  1364. serial->port[1] = tmp;
  1365. serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
  1366. serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
  1367. serial->port[0]->interrupt_in_endpointAddress =
  1368. tmp->interrupt_in_endpointAddress;
  1369. serial->port[1]->interrupt_in_urb = NULL;
  1370. serial->port[1]->interrupt_in_buffer = NULL;
  1371. }
  1372. /* we set up the pointers to the endpoints in the mos7720_open *
  1373. * function, as the structures aren't created yet. */
  1374. /* set up port private structures */
  1375. for (i = 0; i < serial->num_ports; ++i) {
  1376. mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1377. if (mos7720_port == NULL) {
  1378. dev_err(&dev->dev, "%s - Out of memory\n", __func__);
  1379. usb_set_serial_data(serial, NULL);
  1380. kfree(mos7720_serial);
  1381. return -ENOMEM;
  1382. }
  1383. /* Initialize all port interrupt end point to port 0 int
  1384. * endpoint. Our device has only one interrupt endpoint
  1385. * common to all ports */
  1386. serial->port[i]->interrupt_in_endpointAddress =
  1387. serial->port[0]->interrupt_in_endpointAddress;
  1388. mos7720_port->port = serial->port[i];
  1389. usb_set_serial_port_data(serial->port[i], mos7720_port);
  1390. dbg("port number is %d", serial->port[i]->number);
  1391. dbg("serial number is %d", serial->minor);
  1392. }
  1393. /* setting configuration feature to one */
  1394. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1395. (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
  1396. /* LSR For Port 1 */
  1397. send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
  1398. dbg("LSR:%x", data);
  1399. /* LSR For Port 2 */
  1400. send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
  1401. dbg("LSR:%x", data);
  1402. return 0;
  1403. }
  1404. static void mos7720_release(struct usb_serial *serial)
  1405. {
  1406. int i;
  1407. /* free private structure allocated for serial port */
  1408. for (i = 0; i < serial->num_ports; ++i)
  1409. kfree(usb_get_serial_port_data(serial->port[i]));
  1410. /* free private structure allocated for serial device */
  1411. kfree(usb_get_serial_data(serial));
  1412. }
  1413. static struct usb_driver usb_driver = {
  1414. .name = "moschip7720",
  1415. .probe = usb_serial_probe,
  1416. .disconnect = usb_serial_disconnect,
  1417. .id_table = moschip_port_id_table,
  1418. .no_dynamic_id = 1,
  1419. };
  1420. static struct usb_serial_driver moschip7720_2port_driver = {
  1421. .driver = {
  1422. .owner = THIS_MODULE,
  1423. .name = "moschip7720",
  1424. },
  1425. .description = "Moschip 2 port adapter",
  1426. .usb_driver = &usb_driver,
  1427. .id_table = moschip_port_id_table,
  1428. .calc_num_ports = mos77xx_calc_num_ports,
  1429. .open = mos7720_open,
  1430. .close = mos7720_close,
  1431. .throttle = mos7720_throttle,
  1432. .unthrottle = mos7720_unthrottle,
  1433. .probe = mos77xx_probe,
  1434. .attach = mos7720_startup,
  1435. .release = mos7720_release,
  1436. .ioctl = mos7720_ioctl,
  1437. .tiocmget = mos7720_tiocmget,
  1438. .tiocmset = mos7720_tiocmset,
  1439. .set_termios = mos7720_set_termios,
  1440. .write = mos7720_write,
  1441. .write_room = mos7720_write_room,
  1442. .chars_in_buffer = mos7720_chars_in_buffer,
  1443. .break_ctl = mos7720_break,
  1444. .read_bulk_callback = mos7720_bulk_in_callback,
  1445. .read_int_callback = NULL /* dynamically assigned in probe() */
  1446. };
  1447. static int __init moschip7720_init(void)
  1448. {
  1449. int retval;
  1450. dbg("%s: Entering ..........", __func__);
  1451. /* Register with the usb serial */
  1452. retval = usb_serial_register(&moschip7720_2port_driver);
  1453. if (retval)
  1454. goto failed_port_device_register;
  1455. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1456. DRIVER_DESC "\n");
  1457. /* Register with the usb */
  1458. retval = usb_register(&usb_driver);
  1459. if (retval)
  1460. goto failed_usb_register;
  1461. return 0;
  1462. failed_usb_register:
  1463. usb_serial_deregister(&moschip7720_2port_driver);
  1464. failed_port_device_register:
  1465. return retval;
  1466. }
  1467. static void __exit moschip7720_exit(void)
  1468. {
  1469. usb_deregister(&usb_driver);
  1470. usb_serial_deregister(&moschip7720_2port_driver);
  1471. }
  1472. module_init(moschip7720_init);
  1473. module_exit(moschip7720_exit);
  1474. /* Module information */
  1475. MODULE_AUTHOR(DRIVER_AUTHOR);
  1476. MODULE_DESCRIPTION(DRIVER_DESC);
  1477. MODULE_LICENSE("GPL");
  1478. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1479. MODULE_PARM_DESC(debug, "Debug enabled or not");