mos7720.c 41 KB

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