mos7720.c 42 KB

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